简体   繁体   English

从Windows应用程序访问WCF服务方法

[英]Accessing the WCF service method from windows app

I have a WCf service which has this default method 我有一个具有此默认方法的WCf服务

public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

In a windows app i have accessed this method as 在Windows应用程序中,我已经作为

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' 无法将类型'ApplicationName.ServiceReference1.GetDataResponse'隐式转换为'string'

If i use res.ToString() it will print 如果我使用res.ToString()它将打印

ClaimAssistantApplication.ServiceReference1.GetDataResponse ClaimAssistantApplication.ServiceReference1.GetDataResponse

Thats not the string return by my method.I am new to WCF services. 那不是我的方法返回的字符串。我是WCF服务的新手。 is there any method to access the output string? 有什么方法可以访问输出字符串?

Your expectation of how this should work is incorrect. 您对它应该如何工作的期望是不正确的。

If you want to understand why, then have a look at your service WSDL. 如果您想了解原因,请查看您的服务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: 您可以通过使用visual studio命令提示符中的disco.exe工具来执行此操作,该工具会将所有服务元数据下载到目录中:

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. 在服务WSDL中,您将看到其中定义了服务操作的wsdl:operation元素。 Something like: 就像是:

<wsdl:operation name="GetData">

If you look in that element, you should see a wsdl:output message type defined. 如果查看该元素,则应该看到已定义的wsdl:output消息类型。 By convention this will be called: 按照惯例,这称为:

(operation name)Response

So in your instance, the message will be defined as being of type GetDataResponse. 因此,在您的实例中,该消息将被定义为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. 您应该能够GetDataResponse联或一个.xsd文件的形式在下载的服务元数据中找到GetDataResponse类型。

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. 因此,当您将服务引用添加到服务时,实际上是Visual Studio下载了服务元数据,读取了该数据,然后生成了C#代码,该代码允许您调用该服务。 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. 在生成该服务操作时,Visual Studio会发现GetDataResponse XSD类型是GetData服务操作的返回类型,因此它会生成一个名为GetDataResponse的C#类型,并将其分配为Service1Client.GetData和GetDataAsync的返回类型。方法。

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). 如果希望检索操作响应的实际字符串值,则需要深入研究GetDataResponse类型(我相信它将被称为“ Value”,但我不记得了)。

Hope this helps your understanding. 希望这有助于您的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM