从 Web 异常调用 WCF

Calling WCF from web Exception

我是创建 Web 服务的新手,在使用 WCF 创建 Web 服务并将其部署到 IIS 后,我遇到了一些错误,当我尝试使用 Web 浏览器对其进行测试时 returns 我遇到了这个错误我不明白:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.ServiceModel.Description.WsdlExporter.CreateWsdlBindingAndPort(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName, Port& wsdlPort, Boolean& newBinding, Boolean& bindingNameWasUniquified)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
   at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
   at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

网络配置:

<?xml version="1.0"?>
<configuration>

  <connectionStrings>

    <add name="ConnectionString" connectionString=... />

  </connectionStrings>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
    <add key="wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode" value="true" />
  </appSettings>


  <system.web>
    <httpRuntime maxRequestLength="4000480" executionTimeout="600" />
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off" />
    <authentication mode="Windows" />
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
    <sessionState mode="Off" />
  </system.web>




  <system.webServer>



    <directoryBrowse enabled="true" />

  
   
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
        <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>

  </system.webServer>


  <system.serviceModel>
    <services>      

      <service name="CLService.Service1" behaviorConfiguration="Service1Behavior">
        <endpoint address="unsecure/XML" binding="webHttpBinding" bindingConfiguration="webHttpBinding" contract="CLService.IService1" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="unsecure" binding="webHttpBinding" bindingConfiguration="webHttpBinding" contract="CLService.IService1" behaviorConfiguration="RestJSONEndpointBehavior" />

        <endpoint address="xml2" binding="webHttpBinding" bindingConfiguration="webHttpsBinding" contract="CLService.IService1" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="json" binding="webHttpBinding" bindingConfiguration="webHttpsBinding" contract="CLService.IService1" behaviorConfiguration="RestJSONEndpointBehavior" />
      </service>
    </services>
      
  

    <behaviors>
      <endpointBehaviors>

        <behavior name="RestJSONEndpointBehavior">
          <webHttp helpEnabled="false" defaultOutgoingResponseFormat="Json" />
          <!--  <webHttp automaticFormatSelectionEnabled="true" -->

        </behavior>
        <behavior name="RestXMLEndpointBehavior">
          <webHttp helpEnabled="false" defaultOutgoingResponseFormat="Xml" />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="true" />
          <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="500" maxConcurrentInstances="600" />
        </behavior>
        <behavior name="">
          <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="500" maxConcurrentInstances="600" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>


    <bindings>

      <webHttpBinding>
        
        <binding name="webHttpsBinding" crossDomainScriptAccessEnabled="true" maxBufferSize="2000000000" maxReceivedMessageSize="2000000000" transferMode="Buffered" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
          <readerQuotas maxDepth="250000000" maxStringContentLength="250000000" maxArrayLength="250000000" maxBytesPerRead="250000000" maxNameTableCharCount="250000000" />
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>

        <binding name="webHttpBinding" maxBufferPoolSize="9990000" maxBufferSize="2000000000" maxReceivedMessageSize="2000000000" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transferMode="Buffered">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

          <security mode="None" />
        </binding>
      </webHttpBinding>

      <basicHttpBinding>
        <binding name="Service1Soap" />

        <binding name="secureHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>

    </bindings>


    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

</configuration>

我在 IService1 中的服务合同方法:

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Mthod1", BodyStyle = WebMessageBodyStyle.Wrapped)]
    Response Mthod1(String Query);



    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Mthod2",BodyStyle = WebMessageBodyStyle.Wrapped)]
    Response Mthod2(String Query);

我尝试更改行为和绑定,但没有成功

Web 服务使用 https 部署在 IIS 上。

知道可能是什么问题吗? 提前谢谢你。

因为 CLService.IService1 协定在多个端点上公开,客户端应用程序在为协定创建代理实例时必须引用端点名称。如果未确认终结点名称,WCF 将引发错误,因为它不知道要使用哪个终结点。

如果使用浏览器直接打开.svc文件,服务器会报错,因为它不知道你要使用哪个端点。您需要在.svc文件后添加您要访问的端点地址。

经我测试,这个错误不会影响程序的正常访问。这只是服务器不知道您将使用哪个端点的错误。您需要告诉服务您需要访问哪个端点。