11

I have a solution with 3 projects:

  1. ConsoleClient (for testing WCF service)
  2. ServiceLibrary (for WCF)
  3. Web (asp.net mvc project)

I have done some settings in my ServiceLibrary project in 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>

When I run this project, everythings seems normally using wcf test client.

Now, i have also added a WcfDataServiceTest.svc in my Web project(mvc) to host my wcf service.

So, my questions are:

  1. what configuration do I need for my web project (web.config) to actually host this wcf service?
  2. And then I want to run the console app to test it?

Note: i have tested my service using console project but that was getting proxy generation from WCF test client.

By the way, the wcfDataServiceTest.svc file looks like this:

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;
    }
}
4

2 回答 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)));
}
于 2013-03-01T19:09:01.670 回答
0

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

您应该添加:

<endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
于 2013-10-10T16:48:51.170 回答