IIS上托管的WCF服务无法正常工作

编程入门 行业动态 更新时间:2024-10-27 06:27:21
本文介绍了IIS上托管的WCF服务无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to build a service that exposes a basicHTTP endpoint and a webHTTP Endpoint. If i test the following project with VS2010 in running mode everything is fine; but i want to host the service in IIS (local or remotely) and the tests to pass.

Service.svc:

Service.svc:

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

我将网站托管到本地IIS中.当我尝试时: localhost/ContactLibrary2.0/Service.svc 我得到了:

I host my web site into local IIS. When i try : localhost/ContactLibrary2.0/Service.svc i get :

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

The type 'ContactLibrary.ContactLibraryService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

Web.config看起来像:

Web.config looks like :

<?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="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>

IContact看起来像:

IContact looks like :

[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); }

推荐答案

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

In your svc file you need to associate the code behind as well as shown below:

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

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

You dont need to have seperate classes for using BasicHttpBinding and webHttpBinding.

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

Just change your IContact interface to the below:

[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); }

然后将配置中的服务元素更改为:

Then change your service element in your config to :

<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="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>

这样做将公开您的接口IContact,可通过SOAP和REST访问.

Doing so would expose your interface IContact accessible via SOAP and REST.

唯一的更改将是REST端点URL,即localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename

The only change would be to the REST endpoint url which would be localhost/virtualDirectoryname/ContactLibrarySOAPService.svc/rest/resourcename

注意:更改实现IContact的类的名称,以使其具有通用性,而不要使用SOAP或REST来避免混淆.

NOTE: Change the name of the class that implements IContact so as to make it generic rather than having the word SOAP or REST to avoid confusion.

更多推荐

IIS上托管的WCF服务无法正常工作

本文发布于:2023-11-01 22:53:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   IIS   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!