出现了一个奇怪问题
我创建的全部都是新的
重创建一个空的.sln文件开始说
然后创建了一个WCF
最后创建了一个winform窗体
项目结构
WCFApplication.sln空解决方案
MyService WCF服务应用程序
WinForm项目
MyService里面文件
接口服务名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ITestService”。
[ServiceContract]
public interface ITestService
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/ITestService/MyTest", ReplyAction = "http://tempuri.org/ITestService/MyTestResponse")]
string MyTest(string _test);
}
}
实现服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“TestService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 TestService.svc 或 TestService.svc.cs,然后开始调试。
public class TestService : ITestService
{
public string MyTest(string _test)
{
return "Say WCF" + _test;
}
}
}
//在这里我只是声明一个MyTest方法,去实现了这个方法
Winform中代码
问题如下 我在Client(winform)调用时候出现了一个很奇怪的问题
winform
private void Form1_Load(object sender, EventArgs e)
{
ServiceHost serviceHost = new ServiceHost(typeof(Service.TestServiceClient));
serviceHost.AddServiceEndpoint(typeof(Service.ITestService), new WSHttpBinding(), "http://localhost:16053/MyService/TestService");
if (serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
serviceBehavior.HttpGetUrl = new Uri("http://localhost:16053/MyService/TestService/wsdl");
serviceHost.Description.Behaviors.Add(serviceBehavior);
}
serviceHost.Open();
Service.TestServiceClient client = new Service.TestServiceClient();
client.MyTest("test");
//client.MyTestAsync("test");//多出来这个方法。
}
自动调用WCF后生成的App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITestService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:16053/TestService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ITestService" contract="Service.ITestService"
name="BasicHttpBinding_ITestService" />
</client>
</system.serviceModel>
</configuration>
serviceHost.Open()异常
“System.InvalidOperationException”类型的未经处理的异常在 System.ServiceModel.dll 中发生
其他信息: 同一个协定中不能存在两个名称相同的操作,类型为 WindowsFormsApplication1.Service.ITestService 的方法 MyTestAsync 和 MyTest 违反了此规则。可以通过更改方法名称或使用 OperationContractAttribute 的 Name 属性更改其中一个操作的名称。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。