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

11
我有一个包含3个项目的解决方案:
  1. ConsoleClient(用于测试WCF服务)
  2. ServiceLibrary(用于WCF)
  3. Web(asp.net mvc项目)
我已经在我的ServiceLibrary项目中的app.config文件中进行了一些设置。
  <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服务。
因此,我的问题是:
1. 我的Web项目(web.config)需要什么配置才能实际托管这个WCF服务? 2. 然后我想运行控制台应用程序来测试它?
注意:我已经使用控制台项目测试过我的服务,但那是从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;
    }
}
2个回答

16
我正在直接在我的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)));
}

1
我找不到ServiceRoute。有人知道它的命名空间吗? - Rap
1
Rap,ServiceRoute的命名空间(和dll)是System.ServiceModel.Activation。您还需要System.ServiceModel.Web dll来使用WebServiceHostFactory。 - Olivier de Rivoyre

0

这里有一篇博客文章,介绍如何将WCF服务添加到ASP.NET MVC 4应用程序中。

你应该添加:

<endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>

博客文章的链接已经失效。将来请尽量不要添加外部网站的链接。 - ransems
1
@ransems 没有反对外部链接的政策。仅发布链接通常不受欢迎,但添加链接以补充答案或为答案提供更多背景信息是常见做法。 - Tipx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接