Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting a WCF service in an ASP.NET MVC app?

I have a pretty big webapp that's being built in MVC. I'm also abstracting common code into a framework which sits in a separate project. Hopefully this framework will be used in other projects in the near future. There are a few Silverlight apps that are a part of this framework, and one of their jobs is to upload files a chunk at a time. In order to achieve this, I want them to communicate with a WCF service, which also lives in the framework project. I am having problems with this.

I copied the app.config data VS2008 added to my framework project for the service into the web.config, but that didn't seem to work.

After a bit of searching I discovered that you can write a service with a code behind, by creating a .svc file and a matching .cs file, so I tried creating MyService.svc like this:

<% @ServiceHost language="C#"
Service="MyFramework.MyService"
%>

As my service exists within another project, there's no code behind file to reference, so I assumed the Namespace.Class reference would be enough in there.

I also added MyService.svc/{*pathInfo} to the Ignored Routes in my Global.asax file.

However when I try to browse to localhost:x/MyService.svc, or when I try to find the service using the Add Service tool in VS2008, it just seems to hang.

What am I doing wrong?

Anthony

like image 437
littlecharva Avatar asked Aug 13 '09 09:08

littlecharva


1 Answers

Yes well your WCF service is SOAP based - you won't be able to just browse to it and see anything.

If you want to see the service description and all, you'll need to enable the "metadata" exchange by

  • specifying the <serviceMetadata> behavior in your service config
  • defining a "mex" (metadata exchange) endpoint in your service config

To enable the serviceMetadata, you need this section in your service config (web.config - section <system.serviceModel>):

<system.serviceModel>
   <behaviors>
   <serviceBehaviors>
       <behavior name="MEXServiceBehavior">
               <serviceMetadata httpGetEnabled="True"/>
           </behavior>
       </serviceBehaviors>
   </behaviors>

and you'll need to reference that from your service:

<system.serviceModel>
    <service name="....." behaviorConfiguration="MEXServiceBehavior" ....>

To define a MEX endpoint, use something like this:

<services>
    <service name="....." behaviorConfiguration="MEXServiceBehavior" ....>
        <endpoint address="http://localhost:5555/YourSerice/mex"
                  binding="mexHttpBinding" contract="IMetadataExchange" />

There should be plenty of documentation available to show you how to do this (including plenty of questions asked and answered here on Stackoverflow on that topic).

Just a tiny nitpick: you're not really hosting your service "in ASP.NET MVC" - you're hosting it in IIS - the MS web server product. It is totally independent of whether you're using ASP.NET MVC, ASP.NET webforms, or anything else, for that matter.

Marc

like image 121
marc_s Avatar answered Nov 09 '22 23:11

marc_s