views:

1686

answers:

4

Background
I have created ASMX web services in the past and have been able to access the service from the web browser and Ajax GET requests using the address convention:
MyService.asmx/MyMethod?Param=xxx

I just got started using WCF and created a new web service in my ASP.NET project. It creates a file with the .svc extension such as MyService.svc.

Current Situation
I am able to consume the service using the WcfTestClient that comes with VS2008. I am also able to create my own WCF Client by either adding a service reference in another project or using the svcutil.exe commandline to generate the proxy and config file.

The Problem
When I try to use the service from a browser using MyService.svc/MyMethod?MyParam=xxx, I get a blank page without any errors.

What I have tried
I have already added a basicHttpBinding to the web.config and made it HttpGetEnabled in the behavior configuration. I also added the [WebGet(UriTemplate = "MyMethod?MyParam={MyParam}")] attribute to my operation contract.

I have already followed the information in this other stack overflow question:
REST / SOAP EndPoints for a WCF Service

However, I either get a blank page or an HTTP 404 Error after following those steps. There's nothing special about the code. I am just taking in a string as a parameter and returning "Hello xxx". This is a basic "Hello WCF World" proof-of-concept type thing.


UPDATE - Here's the relevant code

[ServiceContract]
public interface IMyService
{
    [WebGet(UriTemplate = "MyMethod/MyParam={MyParam}")]
    [OperationContract]
    string MyMethod(string MyParam);
}

Web.Config - system.serviceModel Section

<system.serviceModel>     
    <behaviors>
     <serviceBehaviors>   
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true"  />
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
     </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="MyService">
     <endpoint address="" 
                       binding="wsHttpBinding" contract="IMyService" />
     <endpoint address="MyService.svc" 
                       binding="basicHttpBinding"  contract="IMyService" />
     <endpoint address="mex" 
                       binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>    
</system.serviceModel>
A: 

The normal WCF requests are always SOAP requests - you won't be able to get this going with just your browser, you'll need the WCF Testclient for that.

There is an add-on for WCF called the WCF REST Starter Kit (which will also be included in WCF 4.0 with .NET 4.0), which allows you to use GET/POST/PUT/DELETE HTTP commands to query WCF services and such. You need to write your services specifically for REST, though - you can't have SOAP and REST on the same service call.

Marc

marc_s
+2  A: 

As marc_s says, the REST Starter Kit can help, but you should also be aware that .NET 3.5 has support for REST services directly in it. It's not quite as complete as what you can do with the starter kit, but it is useful.

The way it works is that you put a [WebGet] attribute on your operations to indicate where in the URL the various parameters should come from:

[WebGet(UriTemplate = "helloworld/{name}")]
string Helloworld(string name);

See this portal for tons of information.

Note, you can have the same service exposed as both SOAP and REST if you specify multiple endpoints/bindings in the configuration.

Erv Walter
I do have the WebGet attribute in my Operation Contract as I mention in my question. I missed the UriTemplate part in my question, but that's how I have it in my code. Have you tried this yourself?
ichiban
Yes, this is how we build our REST services.
Erv Walter
the UriTemplate works for me.
Cheeso
@Erv - I edited my question and posted the contents of the ServiceContract and the serviceModel section of my web.config. Can you see if I'm missing anything. As I said before this works fine from WcfClient, just not from a GET request.
ichiban
The binding you need for REST is webHttpBinding. The bindings you have in your web config are both SOAP (i.e. not GET requests).
Erv Walter
+1  A: 

I highly recommend the book RESTful .NET which is very helpful in this regard.

kenny
+2  A: 

Looking at your web.config serviceModel section, I can see that you need to add a webHttpBinding and associate an endPointBehavior that includes webHttpGet.

Your operation contract is correct. Here's how your system.serviceModel config section should look in order for you to be able to consume the service from a GET HTTP request.

<system.serviceModel>     
    <behaviors>
     <serviceBehaviors>
      <behavior name="MyServiceBehavior">
       <serviceMetadata httpGetEnabled="true"    />
       <serviceDebug includeExceptionDetailInFaults="true"/>          
      </behavior>
     </serviceBehaviors>
      <endpointBehaviors>
     <behavior name="WebBehavior">
       <webHttp />
     </behavior>
      </endpointBehaviors>
    </behaviors>    
    <services>      
      <service behaviorConfiguration="MyServiceBehavior" name="MyService">
     <endpoint address="ws" binding="wsHttpBinding" contract="IMyService"/>
     <endpoint address="" behaviorConfiguration="WebBehavior"
         binding="webHttpBinding"
         contract="IMyService">
     </endpoint>
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>    
</system.serviceModel>

Be sure to assign a different address to your wsHttpBinding endpoint, otherwise you will get an error saying that you have two endpoints listening on the same URI.

Another option is to leave the address blank in the wsHttpBinding, but assign a different address to the webHttpBinding service. However, that will change your GET address as well.

For example, if you assign the address as "asmx", you would call your service with the address "MyService.svc/asmx/MyMethod?MyParam=xxxx".

Jose Basilio
I guess all I needed was to add the endPointBehavior. It worked like a charm. Thank you.
ichiban