C# 在Asp.NETMVC项目中托管WCF服务
我有一个包含3个项目的解决方案:C# 在Asp.NETMVC项目中托管WCF服务,c#,asp.net,asp.net-mvc,wcf,C#,Asp.net,Asp.net Mvc,Wcf,我有一个包含3个项目的解决方案: ConsoleClient(用于测试WCF服务) ServiceLibrary(用于WCF) Web(asp.net mvc项目) 我在app.config中的ServiceLibrary项目中进行了一些设置 <system.serviceModel> <services> <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
<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>
我直接在我的MVC项目中托管一个WCF服务。以下是其结构的一般示例: Web.config服务配置:
以下是服务类:
[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)));
}
是一篇介绍如何将WCF服务添加到ASP.NET MVC 4应用程序的博客文章
您应该添加:
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
我找不到ServiceRoute。有人知道名称空间吗?Rap,ServiceRoute的名称空间(和dll)是System.ServiceModel.Activation。您还需要WebServiceHostFactory的System.ServiceModel.Web dll。指向博客文章的链接已断开。以后请尽量不要添加到外部站点的链接。@ransems没有禁止外部链接的政策。仅仅发布一个链接通常是不受欢迎的,但是添加一个链接来补充答案,或者为答案提供更多的上下文,是常见的做法。
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp />
</behavior>
</endpointBehaviors>