Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting WCF Services in Asp.Net MVC Project

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;
    }
}
like image 281
Idrees Khan Avatar asked Mar 01 '13 17:03

Idrees Khan


1 Answers

I'm hosting a WCF service directly in my MVC project. Below is a generic example of how it's structured:

Web.config service configuration:

<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>

Here's the service class:

[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();
    }
}

And here's where I'm registering it in my 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)));
}
like image 135
KodeKreachor Avatar answered Oct 11 '22 22:10

KodeKreachor