【问题标题】:Hosting WCF Services in Asp.Net MVC Project在 Asp.Net MVC 项目中托管 WCF 服务
【发布时间】:2013-02-16 07:10:25
【问题描述】:

我有 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 project(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;
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc wcf


    【解决方案1】:

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

    【讨论】:

    • 我找不到 ServiceRoute。有人知道命名空间吗?
    • 说唱,ServiceRoute 的命名空间(和 dll)是 System.ServiceModel.Activation。您还需要用于 WebServiceHostFactory 的 System.ServiceModel.Web dll。
    【解决方案2】:

    Here 是一篇博客文章,其中描述了如何将 WCF 服务添加到 ASP.NET MVC 4 应用程序中。

    你应该添加:

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

    【讨论】:

    • 博客文章的链接已损坏。今后请尽量不要添加到外部网站的链接。
    • @ransems 没有针对外部链接的政策。只发布一个链接通常是不受欢迎的,但添加一个链接来补充答案或为答案提供更多上下文是常见的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多