views:

64

answers:

3

I created a WCF service (.NET 3.5) that grabs data from a db and returns a list of objects. It works just fine. I tested it using the WCFTestClient application and got the desired results.

Now, I tried to create an ASP.NET web application and consume the service. After enabling <serviceDebug includeExceptionDetailInFaults="true"/> in the config file, the error message is "Object reference not set to an instance of an object."

How do I modify the service to work with ASP.NET?

Thanks!

Update 1 - I created a reference to the service. When I ran my very basic testing app, based on the sample code displayed on the service's default web page, I got the "Object reference not set to an instance of an object" message.

The service is returning the correct results when I use the WCF Test Client utility provided with Visual Studio. When I try to use the following code, all in the page_load event, I get the object reference message.

PhoneNumberServiceClient client = new PhoneNumberServiceClient();

// Use the 'client' variable to call operations on the service
List<PhoneNumber> phones = client.GetPhoneNumbers();  

// Always close the client.
client.Close();

I will try to get some additional debug info.

A: 

You also have to configure the Service to allow exception details in results. Follow the info in here to make that happen.

I presume your ASP.NET app is expecting to catch certain information from the service that you have not set it up to provide, hence the null object error.

BigChrisDiD
<serviceDebug includeExceptionDetailInFaults="true"/> has been enabled. That's where the Object reference message came from. Before enabling includeExceptionDetailInFaults, I recieved a generic message suggesting I enable it for debug info. The service is returning data, no question about that. I have a breakpoint on the method so I can see what is being returned.
Scott
A: 

I would have written this as comment, but couldn't figure out how to, so I guess I'll attempt an answer.

You mentionned you used the built in test client, but when You created your client application(the website that you want to be calling your new service with) have you configured it's end point(web.config) to match the end of your wcf service? For example, a client app calling a wcf service with wsHttpBinding endpoint:

<configuration>
<system.serviceModel>
    <client>
        <endpoint address="http://localhost:8080/ContractorService" binding="wsHttpBinding" contract="ContractorService.IContractorService"
            name="ContractorService_WsHttp">
        </endpoint>
    </client>
</system.serviceModel>

Also, your service is returning a list, the default service reference that you added to your client very likely stops at array. Right click the service reference your client now has and select configure service reference, in the drop down box for collection type, instead of System.array, select System.Collections.Generic.List.

If you need details on configuration, check here http://msdn.microsoft.com/en-us/library/dd936243.aspx

Place a break point in the Page_Load method of your client, you should be able to step through your client code and it will jump in automatically in your wcf service once you call it's method, then you can see exactly where it fails. Hope this helps

GenEric35
I tried changing the collection type between list and array. No go.I've changed so much stuff at this point, I can't get anything to work. In the morning, I'm gonna start over and see what happens.
Scott
+2  A: 

Have you tried openning the service after you initialize it by using:

client.Open();

If the problem still persists maybe the it lies on the WCF method itself. Try to add breakpoints on your method and debug it to trace the cause of the error. You could also use profilers.

Jojo Sardez
Wrapped various lines with try-catch blocks for debugging. I was able to open the connection but no data was being returned by the method that should've returned a list of objects. Further investigation revealed I had a bad web.config file.
Scott