我是新加入WCF的,我有一个服务,我想在控制台应用程序中使用它,我运行svcutil.exe svcutil.exe并给我两个输出,一个是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();
}发布于 2019-09-26 08:06:37
SVCUtil.exe生成客户端代理类和服务端点地址。如果我们只想通过使用这些文件来调用服务。我们只需要将客户端代理类(testService.cs)添加到控制台应用程序中,并将output.config文件中的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>然后实例化客户端代理,通过智能提示,得到包含在代理类中的服务方法。就像我们称之为本地方法一样。
Service1Client client = new Service1Client("BasicHttpsBinding_IService1");
try
{
var result = client.GetData(34);
Console.WriteLine(result);
}
catch (Exception)
{
throw;
}我们需要注意的另一件事是,在进行调用时,确保服务处于运行状态。
如果问题还存在,请随时告诉我。
https://stackoverflow.com/questions/58101157
复制相似问题