出現(xiàn)了一個(gè)奇怪問(wèn)題
我創(chuàng)建的全部都是新的
重創(chuàng)建一個(gè)空的.sln文件開(kāi)始說(shuō)
然后創(chuàng)建了一個(gè)WCF
最后創(chuàng)建了一個(gè)winform窗體
項(xiàng)目結(jié)構(gòu)
WCFApplication.sln空解決方案
MyService WCF服務(wù)應(yīng)用程序
WinForm項(xiàng)目
MyService里面文件
接口服務(wù)名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyService
{
// 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼和配置文件中的接口名“ITestService”。
[ServiceContract]
public interface ITestService
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/ITestService/MyTest", ReplyAction = "http://tempuri.org/ITestService/MyTestResponse")]
string MyTest(string _test);
}
}
實(shí)現(xiàn)服務(wù)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyService
{
// 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼、svc 和配置文件中的類名“TestService”。
// 注意: 為了啟動(dòng) WCF 測(cè)試客戶端以測(cè)試此服務(wù),請(qǐng)?jiān)诮鉀Q方案資源管理器中選擇 TestService.svc 或 TestService.svc.cs,然后開(kāi)始調(diào)試。
public class TestService : ITestService
{
public string MyTest(string _test)
{
return "Say WCF" + _test;
}
}
}
//在這里我只是聲明一個(gè)MyTest方法,去實(shí)現(xiàn)了這個(gè)方法
Winform中代碼
問(wèn)題如下 我在Client(winform)調(diào)用時(shí)候出現(xiàn)了一個(gè)很奇怪的問(wèn)題
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");//多出來(lái)這個(gè)方法。
}
自動(dòng)調(diào)用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”類型的未經(jīng)處理的異常在 System.ServiceModel.dll 中發(fā)生
其他信息: 同一個(gè)協(xié)定中不能存在兩個(gè)名稱相同的操作,類型為 WindowsFormsApplication1.Service.ITestService 的方法 MyTestAsync 和 MyTest 違反了此規(guī)則??梢酝ㄟ^(guò)更改方法名稱或使用 OperationContractAttribute 的 Name 屬性更改其中一個(gè)操作的名稱。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。