Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在ASP.NET网站中托管时出现WCF服务异常错误_C#_Wcf_.net 4.0_Exception Handling_Wcf Configuration - Fatal编程技术网

C# 尝试在ASP.NET网站中托管时出现WCF服务异常错误

C# 尝试在ASP.NET网站中托管时出现WCF服务异常错误,c#,wcf,.net-4.0,exception-handling,wcf-configuration,C#,Wcf,.net 4.0,Exception Handling,Wcf Configuration,尝试浏览我的.svc文件时发生以下错误 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.Cre

尝试浏览我的.svc文件时发生以下错误

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)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
   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 System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
   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.ProcessMessage41(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
以下是我的web.config配置:

  <system.serviceModel>
    <services>
      <service name="WcfServiceLib.WcfService"
               behaviorConfiguration="Default">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/WebService"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="wsHttpBinding" contract="WcfServiceContractLib.IWcfContract"/>
        <endpoint address="web" binding="webHttpBinding" contract="WcfServiceContractLib.IWcfContract"
                  behaviorConfiguration="web"/>
        <endpoint address="webJson" binding="webHttpBinding" contract="WcfServiceContractLib.IWcfContract"
                behaviorConfiguration="webJson"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp defaultOutgoingResponseFormat="Xml" helpEnabled="true"/>
        </behavior>
        <behavior name="webJson">
          <webHttp defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled ="true" />
  </system.serviceModel>

有人有类似的情况吗

我应该提到,当它托管在控制台应用程序中时,我用来运行测试,一切都很好


[编辑]尝试在控制台应用程序中加载服务时发生相同错误。不显示可以查看wsdl信息等的WCF网页。

如果您使用.NET 4.0并试图同时托管SOAP、Xml和Json端点,则会出现异常。只需输入Json或Xml端点,就可以了

这是我提出的一个已知问题,微软开发团队已经解决了这个问题,因为他们无法解决


为了支持Json和Xml,如果客户机只在请求中传递所需的Accept头,如“application/Xml”或“application/Json”,框架将自动处理它并以适当的格式发送回响应

这就是我的工作:

在VS2012中创建WCF类库项目

数据类型的Xsd架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="urn:wcf:hydra:datatypes.xsd:1.5" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:wcf:hydra:datatypes.xsd:1.5" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="IncomingDocument">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Firstname" type="xs:string"/>
                <xs:element name="Surname" type="xs:string"/>
                <xs:element name="ChildrensNames" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ChildsName" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Gender" minOccurs="0">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="Male"/>
                            <xs:enumeration value="Female"/>
                            <xs:enumeration value="Other"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="Version" type="xs:string" use="required" fixed="1.5"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="ResponseDocument">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="NumberOfChildren" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="DayDetails">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="CurrentDateTime" type="xs:dateTime"/>
                <xs:element name="WeatherDetails" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
使用两个接口实现您的具体类:

public sealed class TestMutliHeadService : ITestMutliHeadService, ITestMutliHeadServiceSchemaCompliant
{
    public ResponseDocument LoadChildren(IncomingDocument request)
    {
        int count = 0;
        if (request.ChildrensNames != null)
        {
            count = request.ChildrensNames.Count();
        }

        return new ResponseDocument()
        {
            NumberOfChildren = count,
        };
    }

    public DayDetails DayDetails()
    {
        return new DayDetails()
        {
            CurrentDateTime = DateTime.Now,
            WeatherDetails = "It's rather nice, really.",
        };
    }

    public string Test(string input)
    {
        return input;
    }
}
现在创建一个空白的ASP.NET项目,并添加一个
web.config
和一个
Global.asax
以及一个对
System.ServiceModel.Activation

Global.asax.cs:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        var factory = new ServiceHostFactory();
        RouteTable.Routes.Add(new ServiceRoute("", factory, typeof(TestMutliHeadService)));
    }
}
这是神奇的怪物,web.config中的system.serviceModel:

<system.serviceModel>
    <!-- Services -->
    <services>
      <service name="WCF.Hydra.BL.TestMutliHeadService">
        <endpoint name="restJson"       address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="restJsonSecure" address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="restXml"        address="rest/xml"  behaviorConfiguration="restXmlEndpointBehaviour"  binding="webHttpBinding"    bindingConfiguration="restBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
        <endpoint name="restXmlSecure"  address="rest/xml"  behaviorConfiguration="restXmlEndpointBehaviour"  binding="webHttpBinding"    bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
        <endpoint name="mex"            address="mex"       behaviorConfiguration=""                          binding="mexHttpBinding"    bindingConfiguration="mexBindingConfig"        contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="mexSecure"      address="mex"       behaviorConfiguration=""                          binding="mexHttpsBinding"   bindingConfiguration="mexSecureBindingConfig"  contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="soapXsd"        address="soap"      behaviorConfiguration=""                          binding="basicHttpBinding"  bindingConfiguration="soapBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="soapXsdSecure"  address="soap"      behaviorConfiguration=""                          binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
      </service>
    </services>
    <!--  Binding Configurations -->
    <bindings>
      <webHttpBinding>
        <binding name="restBindingConfig">
          <security mode="None"/>
        </binding>
        <binding name="restSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
      <mexHttpBinding>
        <binding name="mexBindingConfig"/>
      </mexHttpBinding>
      <mexHttpsBinding>
        <binding name="mexSecureBindingConfig"/>
      </mexHttpsBinding>
      <basicHttpsBinding>
        <binding name="soapSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </basicHttpsBinding>
      <basicHttpBinding>
        <binding name="soapBindingConfig">
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <!-- Behaviour Configurations -->
    <behaviors>
      <endpointBehaviors>
        <behavior name="restJsonEndpointBehaviour">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
        </behavior>
        <behavior name="restXmlEndpointBehaviour">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Xml" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- Hosting Environment Settings -->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>


浏览到/soap查找soap,/rest/json查找restful json,/rest/xml查找与模式兼容的ish-xml。

Thanx,您在发布答案之前就找到了我,您应该得到分数。我删除了第二个端点,让它使用基于Accept、Content-Type的自动格式选择。现在很好用。我也为REST操作创建了一个不同的接口定义,但具体实现暂时保持不变。@taskosgorge/@Rajesh:我正在搜索相同问题的答案。正如您所说,我的配置文件中没有指定任何端点。你能告诉我背后的原因吗?
<system.serviceModel>
    <!-- Services -->
    <services>
      <service name="WCF.Hydra.BL.TestMutliHeadService">
        <endpoint name="restJson"       address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="restJsonSecure" address="rest/json" behaviorConfiguration="restJsonEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="restXml"        address="rest/xml"  behaviorConfiguration="restXmlEndpointBehaviour"  binding="webHttpBinding"    bindingConfiguration="restBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
        <endpoint name="restXmlSecure"  address="rest/xml"  behaviorConfiguration="restXmlEndpointBehaviour"  binding="webHttpBinding"    bindingConfiguration="restSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadServiceSchemaCompliant"/>
        <endpoint name="mex"            address="mex"       behaviorConfiguration=""                          binding="mexHttpBinding"    bindingConfiguration="mexBindingConfig"        contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="mexSecure"      address="mex"       behaviorConfiguration=""                          binding="mexHttpsBinding"   bindingConfiguration="mexSecureBindingConfig"  contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="soapXsd"        address="soap"      behaviorConfiguration=""                          binding="basicHttpBinding"  bindingConfiguration="soapBindingConfig"       contract="WCF.Hydra.BL.ITestMutliHeadService"/>
        <endpoint name="soapXsdSecure"  address="soap"      behaviorConfiguration=""                          binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="WCF.Hydra.BL.ITestMutliHeadService"/>
      </service>
    </services>
    <!--  Binding Configurations -->
    <bindings>
      <webHttpBinding>
        <binding name="restBindingConfig">
          <security mode="None"/>
        </binding>
        <binding name="restSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
      <mexHttpBinding>
        <binding name="mexBindingConfig"/>
      </mexHttpBinding>
      <mexHttpsBinding>
        <binding name="mexSecureBindingConfig"/>
      </mexHttpsBinding>
      <basicHttpsBinding>
        <binding name="soapSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </basicHttpsBinding>
      <basicHttpBinding>
        <binding name="soapBindingConfig">
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <!-- Behaviour Configurations -->
    <behaviors>
      <endpointBehaviors>
        <behavior name="restJsonEndpointBehaviour">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
        </behavior>
        <behavior name="restXmlEndpointBehaviour">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Xml" automaticFormatSelectionEnabled="false" faultExceptionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- Hosting Environment Settings -->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>