繁体   English   中英

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

[英]Hosting WCF Services in Asp.Net MVC Project

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

  1. ConsoleClient(用于测试WCF服务)
  2. ServiceLibrary(用于WCF)
  3. 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 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;
    }
}

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

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

你应该添加:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM