C# IIS上承载的WCF服务不工作

C# IIS上承载的WCF服务不工作,c#,json,wcf,soap,C#,Json,Wcf,Soap,我想构建一个公开basicHTTP端点和webHTTP端点的服务。如果我在运行模式下用VS2010测试以下项目,一切都很好;但是我想在IIS中托管服务(本地或远程)并通过测试 Service.svc: <%@ ServiceHost Language="C#" Debug="true" Service="ContactLibrary.ContactLibraryService"%> 我将我的网站托管到本地IIS中。当我尝试时:我得到: 找不到作为ServiceHost指令中的服

我想构建一个公开basicHTTP端点和webHTTP端点的服务。如果我在运行模式下用VS2010测试以下项目,一切都很好;但是我想在IIS中托管服务(本地或远程)并通过测试

Service.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibrary.ContactLibraryService"%>

我将我的网站托管到本地IIS中。当我尝试时:我得到:

找不到作为ServiceHost指令中的服务属性值提供的或在配置元素system.serviceModel/serviceHostingEnvironment/serviceActivations中提供的类型“ContactLibrary.ContactLibraryService”

Web.config看起来像:

<?xml version="1.0"?>
<configuration>    
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ContactLibraryNamespace.ContactLibraryService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
          name="soap" contract="ContactLibraryNamespace.IContact" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          name="mex" contract="IMetadataExchange" />
        <endpoint address="rest" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="" name="rest" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ContactLibrary2.0" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>      
</configuration>
[ServiceContract]
    public interface IContact
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetContact/{idContact}", ResponseFormat = WebMessageFormat.Json)]
        Contact GetContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddContact", RequestFormat = WebMessageFormat.Json)]
        string AddContact(Contact contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "EditContact", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string EditContact(string idContact, Contact Contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DeleteContact", RequestFormat = WebMessageFormat.Json)]
        string DeleteContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetAllContacts/{start}/{end}", RequestFormat = WebMessageFormat.Json)]
        List<Contact> GetAllContacts(string start, string end);        
    }

IContact看起来像:

<?xml version="1.0"?>
<configuration>    
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ContactLibraryNamespace.ContactLibraryService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
          name="soap" contract="ContactLibraryNamespace.IContact" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          name="mex" contract="IMetadataExchange" />
        <endpoint address="rest" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="" name="rest" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ContactLibrary2.0" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>      
</configuration>
[ServiceContract]
    public interface IContact
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetContact/{idContact}", ResponseFormat = WebMessageFormat.Json)]
        Contact GetContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddContact", RequestFormat = WebMessageFormat.Json)]
        string AddContact(Contact contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "EditContact", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string EditContact(string idContact, Contact Contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DeleteContact", RequestFormat = WebMessageFormat.Json)]
        string DeleteContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetAllContacts/{start}/{end}", RequestFormat = WebMessageFormat.Json)]
        List<Contact> GetAllContacts(string start, string end);        
    }
[服务合同]
公共接口IContact
{
[经营合同]
[WebInvoke(Method=“GET”,UriTemplate=“GetContact/{idContact}”,ResponseFormat=WebMessageFormat.Json)]
触点GetContact(字符串idContact);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“AddContact”,RequestFormat=WebMessageFormat.Json)]
串加触点(触点触点触点);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“EditContact”,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
字符串编辑触点(字符串ID触点,触点触点);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“deleteecontact”,RequestFormat=WebMessageFormat.Json)]
字符串删除触点(字符串ID触点);
[经营合同]
[WebInvoke(Method=“GET”,UriTemplate=“GetAllContacts/{start}/{end}”,RequestFormat=WebMessageFormat.Json)]
列出GetAllContacts(字符串开始、字符串结束);
}

在svc文件中,您需要关联隐藏的代码,如下所示:

<%@ ServiceHost Language="C#" Debug="true" Service="ContactLibraryNamespace.ContactLibrarySOAPService" CodeBehind="ContactLibrarySOAPService.svc.cs" %>

使用BasicHttpBinding和webHttpBinding不需要单独的类

只需将IContact界面更改为以下内容:

[ServiceContract]
    public interface IContact
    {
        [OperationContract]
        [WebInvoke(Method="GET", UriTemplate = "GetContact/{idContact}", ResponseFormat=WebMessageFormat.Json)]
        Contact GetContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddContact", RequestFormat = WebMessageFormat.Json)]
        string AddContact(Contact contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "EditContact", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string EditContact(string idContact, Contact Contact);
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DeleteContact", RequestFormat = WebMessageFormat.Json)]
        string DeleteContact(string idContact);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetAllContacts/{start}/{end}", RequestFormat = WebMessageFormat.Json)]
        List<Contact> GetAllContacts(string start, string end);        
    }
[服务合同]
公共接口IContact
{
[经营合同]
[WebInvoke(Method=“GET”,UriTemplate=“GetContact/{idContact}”,ResponseFormat=WebMessageFormat.Json)]
触点GetContact(字符串idContact);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“AddContact”,RequestFormat=WebMessageFormat.Json)]
串加触点(触点触点触点);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“EditContact”,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
字符串编辑触点(字符串ID触点,触点触点);
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“deleteecontact”,RequestFormat=WebMessageFormat.Json)]
字符串删除触点(字符串ID触点);
[经营合同]
[WebInvoke(Method=“GET”,UriTemplate=“GetAllContacts/{start}/{end}”,RequestFormat=WebMessageFormat.Json)]
列出GetAllContacts(字符串开始、字符串结束);
}
然后将配置中的服务元素更改为:

<system.serviceModel>
    <services>
      <service name="ContactLibraryNamespace.ContactLibrarySOAPService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
         contract="ContactLibraryNamespace.IContact" />
        <endpoint address="rest" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="" contract="ContactLibraryNamespace.IContact" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
         contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ContactLibrary2.0" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
       <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
        <behavior name="json">
          <enableWebScript />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

这样做将暴露可通过SOAP和REST访问的接口IContact

唯一的更改是REST端点url,它将是
http://localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename


注意:更改实现IContact的类的名称,使其成为泛型,而不是使用SOAP或REST一词,以避免混淆。

能否发布IContact接口?一切看起来都很好。但我没有看到ContactLibraryJSONService的端点。您是否定义了这样的端点?如果没有,你应该添加它。我还没有添加一个,因为我想看到这个部分工作。我希望看到我的服务响应WCFStorm。现在有了一个json端点,项目仍然无法工作。知道罗克和帕拉莫什吗?谁是“json”?我没有任何代码隐藏,在*.svc下也没有任何cs类。我没有使用代码隐藏,而是使用对我的C#项目的引用。这是一件错误/坏事吗?我用最新的编辑/错误编辑了我的问题到我的项目中。不,这应该是好的,只要在代码隐藏属性中使用名称空间的类名作为值,然后尝试项目的名称是“ContactLibrary”,但名称空间是“ContactLibraryNamespace”。我的错误。