C# · 12月 25, 2021

c# – 在Asp.Net MVC项目中托管WCF服务

我有一个解决方案有3个项目:

> ConsoleClient(用于测试WCF服务)
> ServiceLibrary(适用于WCF)
> Web(asp.net mvc项目)

我在app.config中的ServiceLibrary项目中做了一些设置

<system.serviceModel> <services> <service name=”MrDAStoreJobs.ServiceLibrary.AdvertisementService”> <clear /> <endpoint address=”http://localhost:8050/ServiceLibrary/basic” binding=”basicHttpBinding” bindingConfiguration=”” contract=”MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService” /> <host> <baseAddresses> <add baseAddress=”http://localhost:8050/Design_Time_Addresses/MrDAStoreJobs/ServiceLibrary/AdvertisementService/” /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!– To avoid disclosing Metadata information,set the value below to false 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> </system.serviceModel>

当我运行这个项目时,似乎通常使用wcf测试客户端.

现在,我还在我的Web项目(mvc)中添加了一个WcfDataServiceTest.svc来托管我的wcf服务.

所以我的问题是:

>我的web项目(web.config)实际托管这个wcf服务需要什么配置?
>然后我想运行控制台应用程序来测试它?

注意:我已经使用控制台项目测试了我的服务,但正在从WCF测试客户端获取代理生成.

顺便说一下,wcfDataServiceTest.svc文件如下所示:

public class WcfDataServiceTest : DataService<AdvertisementService>{ // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { // TODO: set rules to indicate which entity sets and service operations are visible,updatable,etc. // Examples: config.SetEntitySetAccessRule(“Advertisements”,EntitySetRights.AllRead); // config.SetServiceOperationAccessRule(“MyServiceOperation”,ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; }}解决方法 我在我的MVC项目中直接托管一个WCF服务.下面是一个通用的例子:

Web.config服务配置:

<system.serviceModel><behaviors> <serviceBehaviors> <behavior name=””> <serviceMetadata httpGetEnabled=”true” /> <serviceDebug includeExceptionDetailInFaults=”false” /> </behavior> </serviceBehaviors></behaviors><bindings> <customBinding> <binding name=”customBinding0″> <binaryMessageEncoding /> <httpTransport /> </binding> </customBinding></bindings><serviceHostingEnvironment aspNetCompatibilityEnabled=”true” multipleSiteBindingsEnabled=”true” /><standardEndpoints> <webHttpEndpoint> <standardEndpoint name=”” helpEnabled=”true” defaultOutgoingResponseFormat=”Json” automaticFormatSelectionEnabled=”true” maxReceivedMessageSize=”1048576″> <readerQuotas maxStringContentLength=”1048576″ /> </standardEndpoint> </webHttpEndpoint></standardEndpoints></system.serviceModel>

这是服务类:

[ServiceContract][ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class MyService{ [OperationContract] [WebInvoke(UriTemplate = “{personId}”,Method = “GET”)] public Person Get(string personId) { return new Person(); }}

这里是我在我的MVC Global.asax中注册的地方

public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); RouteTable.Routes.Add(new ServiceRoute(“SVC/My”,new WebServiceHostFactory(),typeof(MyService)));}