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;
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Idrees Khan
  • 7,702
  • 18
  • 63
  • 111

2 Answers2

16

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)));
}
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
0

Here is a blog post with a description of how to add WCF service in to the ASP.NET MVC 4 application.

You should add:

<endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
druss
  • 1,820
  • 19
  • 18
  • The link to the blog post is broken. In the future please try not to add links to external sites. – ransems Dec 04 '18 at 14:55
  • 1
    @ransems There is no policy against external links. Posting JUST a link is usually frowned upon, but adding a link to complement an answer, or provide more context to the answer, is common practice. – Tipx Jun 12 '19 at 18:16