如何使用 svcutil.exe 生成的 WCF 服务

how to use generated WCF service from svcutil.exe

我是 WCF 的新手,我有一个服务,我想在控制台应用程序中使用它,我 运行 svcutil.exe htt://localhost:58221 /myservice.svc/mex 并给了我两个输出,一个是 cs 文件,另一个是 XML,我将配置复制并粘贴到我的应用程序设置中,但我没有不知道我还应该使用 CS 文件吗?以及如何?它给了我这样的例子:

   class Test
  {
    static void Main()
    {
   MyServiceServiceClient client = new MyServiceServiceClient ();

    // Use the 'client' variable to call operations on the service.

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

SVCUtil.exe 生成客户端代理 class 和服务端点地址。如果我们只是想通过使用这些文件来调用服务。我们只需要将客户端代理 class (testService.cs) 添加到控制台应用程序并复制 System.servicemode 部分到 output.config文件到控制台应用程序中的 app.config 文件。

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ITestService" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4386/" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_ITestService" contract="ITestService"
            name="BasicHttpBinding_ITestService" />
    </client>
</system.serviceModel>

然后实例化客户端代理,通过智能提示,我们可以得到代理中包含的服务方法class。就像我们调用本地方法一样。

Service1Client client = new Service1Client("BasicHttpsBinding_IService1");
            try
            {
                var result = client.GetData(34);
                Console.WriteLine(result);
            }
            catch (Exception)
            {

                throw;
            }

我们还需要注意的一点是确保调用时服务处于运行状态。
如果问题仍然存在,请随时告诉我。