Reputation: 43
I have a WCf service which has this default method
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
In a windows app i have accessed this method as
private async void btnLogin_Click_1(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
var res = await client.GetDataAsync(78);
txtUsername.Text = res;
}
As you can see in my method I i am returning a string value. But when i try to dispaly it in the textbox it gives the error
Cannot implicitly convert type 'ApplicationName.ServiceReference1.GetDataResponse' to 'string'
If i use res.ToString()
it will print
ClaimAssistantApplication.ServiceReference1.GetDataResponse
Thats not the string return by my method.I am new to WCF services. is there any method to access the output string?
Upvotes: 0
Views: 230
Reputation: 31760
Your expectation of how this should work is incorrect.
If you want to understand why, then have a look at your service WSDL. You can do this by using the disco.exe tool in the visual studio command prompt, which will download all the service metadata to a directory:
disco /out:myOutputDir http://MyServiceAddress
In your service WSDL, you will see that there is a wsdl:operation
element in there which defines your service operation. Something like:
<wsdl:operation name="GetData">
If you look in that element, you should see a wsdl:output
message type defined. By convention this will be called:
(operation name)Response
So in your instance, the message will be defined as being of type GetDataResponse. This is the actual type returned when you consume and call the service operation, as defined by the service metadata.
In fact, if you use fiddler or something similar to call the service operation, you should see the actual response message being returned. It will look something like this:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:GetDataResponse xmlns:m="YourServiceNamespace">
<getData>You entered: 78</getData>
</m:GetDataResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You should be able to locate the GetDataResponse
type in the service metadata you downloaded, either inline, or in one of the .xsd files.
So when you add a service reference to your service, what is happening is that visual studio downloads the service metadata, reads it, and then generates the C# code which allows you to call the service. When it comes to generating that service operation, visual studio sees that the GetDataResponse XSD type is the return type of your GetData service operation, so it generates a C# type called GetDataResponse, and assigns it as the return type of the Service1Client.GetData and GetDataAsync methods.
If you wish to retrieve the actual string value of your operation response, then you need to drill into the GetDataResponse type (I believe it will be called "Value", but I can't remember).
Hope this helps your understanding.
Upvotes: 1