如何使用 WCF 服务

How to consume WCF service

我是 C# 开发的新手,作为第一个任务,我被分配了 WCF 服务,并在代码项目中尝试了一些示例。 现在我正在处理复杂类型,但无法获得响应。 从 wsdl 文件和 xsd 使用 svcutil.exe wsdlname xsd 并根据这些文件得到两个文件在本地创建一个服务并尝试以下面的方式使用。

下面是界面

我能够 运行 该服务能够看到该服务 url 并且能够在我的客户端中引用。

下面是我正在尝试从客户端调用服务到这个存根,但无法理解如何调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                 ServiceReference1.sendMessageResponse1 s = new
ServiceReference1.sendMessageResponse1();
                 ServiceReference1.sendMessageResponse1 s1 = new ServiceReference1.sendMessageResponse1();
                 //s1.messageid = 1;
                 //s1.recipient = "Chiranjeevi";
                 //s1.status = "Sent";
                 //ServiceReference1.sendMessageResponse ss= (ServiceReference1.sendMessageResponse) s1;
                 Console.Read();            
             }
        }
    }

但是在尝试调用该服务时,我也没有得到任何默认构造函数的输出。尝试使用 console.writeline();

我正在尝试调用 sendMessageResponse1 sendMessage(sendMessageRequest 请求);来自服务。

尝试调用上述方法时出现以下错误。 错误 1 ​​无法将类型 'ConsoleApplication3.ServiceReference1.sendMessageResponse1' 转换为 'ConsoleApplication3.ServiceReference1.sendMessageResponse'

好吧,您正在尝试将一个 class 的实例转换为另一个实例。

ServiceReference1.sendMessageResponse1 s = 
    new ServiceReference1.sendMessageResponse1();
ServiceReference1.sendMessageResponse1 s1 = 
    new ServiceReference1.sendMessageResponse1();

//s1.messageid = 1;
//s1.recipient = "Chiranjeevi";
//s1.status = "Sent";
//ServiceReference1.sendMessageResponse ss=
//    (ServiceReference1.sendMessageResponse) s1;

下面这行代码会中断,因为 class sendMessageRequestsendMessageRequest1 之间没有转换。

ServiceReference1.sendMessageResponse ss=
    (ServiceReference1.sendMessageResponse) s1;

这是交易:

  • s1 属于 sendMessageResponse1.
  • 类型
  • 您试图将其转换为 sendMessageResponse 的类型。
  • 那是行不通的,因为前者不是从后者派生出来的。

这正是错误告诉您的内容:

Error 1 Cannot convert type 'ConsoleApplication3.ServiceReference1.sendMessageResponse1' to 'ConsoleApplication3.ServiceReference1.sendMessageResponse'

经过大量谷歌搜索后得到以下内容url,这是最好的初学者,可以在没有 WCF 知识的情况下直接投入项目。 最佳起点文章。

https://www.packtpub.com/books/content/implementing-wcf-service-real-world