tags:

views:

26

answers:

1

Hello,

I have created a brand new WCF Service. I created this service by just saying Add New Item... -> WCF Service in Visual Studio. I then edited the contract slightly to look like the following:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebGet(UriTemplate = "/Authenticate/{username}/{password}", ResponseFormat = WebMessageFormat.Json)]
    bool Authenticate(string username, string password);
}

My operations looks like the following:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
public class MyService : IMyService
{
    public bool Authenticate(string username, string password)
    {
        try
        {
            return false;
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Unknown exception");
        }
    }
}

When I visit: http://localhost:80/MyService.svc/Authenticate/someUserName/somePassword in my browser window, an empty screen appears. I was expecting "false" to appear in JSON syntax. What am I doing wrong?

Thank you!

A: 

Use a tool like Fiddler to see the actual HTTP messages. Helps with debugging.

Second, you're request URL is wrong. Try this:

http://localhost:80/MyService.svc/Authenticate/someUserName/somePassword

You do have a SVC file, correct? You'll need that if you're hosting this in IIS. If you're self hosting it in a WebServiceHost object, then you don't need them.

    using( WebServiceHost host = new WebServiceHost( typeof( MyService) ) )
    {
        host.Open();
        Console.WriteLine( "Service is running" );
        Console.WriteLine( "Press enter to quit..." );
        Console.ReadLine();
        host.Close();
    }
MonkeyWrench
You are correct. I do have an SVC file. I have since edited the post. I still get a blank screen though.
Villager
Blank screen? You're using Firefox to test this? You're probably getting a 400 or 404 error. IE (yes, IE) will give you the HTTP error. Also try Fiddler. Very good tool. http://www.fiddler2.com/fiddler2/
MonkeyWrench
How are you hosting the service? In IIS? Visual Studio Dev server thing? Self hosting with a WebServiceHost? If find creating a console application and using it to self host the web application makes debugging easier.
MonkeyWrench
After I installed fiddler, I discovered that I'm getting a 400 error. But I don't understand what I'm doing wrong. Thank you.
Villager
How are you hosting the service? Post your config file, please. Another thing that has caught me in the past was a problem in the project file. Open up the project file and make sure this element is empty: <IISUrl></IISUrl>
MonkeyWrench
Got it. I added Factory="System.ServiceModel.Activation.WebServiceHostFactory" in the .svc file. Thank you for your help.
Villager
Glad you got it working. WCF can be frustrating sometimes.
MonkeyWrench