[英]windows form application calling a WCF service and the WCF service calling another WCF service
這是我為測試服務而建立的示例項目,該服務調用了另一個服務
這是其他服務調用的服務
namespace WCFPub
{
[ServiceContract]
public interface IStudent
{
[OperationContract]
string getName(string name);
}
}
namespace WCFPub
{
public class Student : IStudent
{
public string getName(string name)
{
return "Your name is " + name;
}
}
}
承載上述服務的控制台應用程序
namespace WCFHost
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceHost sh = new ServiceHost(typeof(WCFPub.Student));
ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
};
sh.Description.Behaviors.Add(serviceMetadataBehaviour);
sh.AddServiceEndpoint(typeof(WCFPub.IStudent), new WSDualHttpBinding(), "PS");
Console.WriteLine("Host Ready, Listening on 7060");
Console.WriteLine("Hit Enter to Stop..");
sh.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
調用第二項服務的服務
namespace WCFPub2
{
[ServiceContract]
public interface IMaster
{
[OperationContract]
string getNameFromStudent(string name);
}
}
namespace WCFPub2
{
public class Master : IMaster
{
public string getNameFromStudent(string name)
{
Proxy2.StudentClient client = new Proxy2.StudentClient();
return client.getName("ABdi");
}
}
}
托管上述服務的控制台應用程序
namespace WCFHost2
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceHost sh = new ServiceHost(typeof(WCFPub2.Master));
ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
};
sh.Description.Behaviors.Add(serviceMetadataBehaviour);
sh.AddServiceEndpoint(typeof(WCFPub2.IMaster), new WSDualHttpBinding(), "PS");
Console.WriteLine("Host Ready, Listening on 7061");
Console.WriteLine("Hit Enter to Stop..");
sh.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
客戶端
namespace WCFClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Proxy.MasterClient client = new Proxy.MasterClient();
MessageBox.Show(client.getNameFromStudent("ABdi"));
}
}
}
這不起作用並引發異常
System.ServiceModel.FaultException`1 [System.ServiceModel.ExceptionDetail]:
在ServiceModel客戶端配置部分中找不到引用合同'Proxy2.IStudent'的默認終結點元素。 這可能是因為找不到您的應用程序的配置文件,或者是因為在客戶端元素中找不到與該協定匹配的端點元素。
(故障詳細信息等於ExceptionDetail,可能由IncludeExceptionDetailInFaults = true創建,其值為:
System.InvalidOperationException:在ServiceModel客戶端配置部分中找不到引用合同'Proxy2.IStudent'的默認終結點元素。 這可能是因為找不到您的應用程序的配置文件,或者是因為在客戶端元素中找不到與該協定匹配的端點元素。在System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,字符串configurationName)
在System.ServiceModel.ChannelFactory.ApplyConfiguration處(字符串configurationName,配置配置)
在System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
在System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串configurationName,EndpointAddress地址)
在System.ServiceModel.ChannelFactory`1..ctor(字符串endpointConfigurationName,EndpointAddress remoteAddress)
在System.ServiceModel.Configu ...)。
我需要幫助
我沒有看到你指定服務和客戶端地址(我沒有看到前主機=新的ServiceHost(新Service.BossService() 的baseUrl);或新Proxy.MasterClient(endpointConfiguraionName)或Proxy.MasterClient(綁定,baseAddress))。 如果您到處都在使用配置文件(其中有地址),則不需要任何其他步驟來設置服務(Behaviors.Add,... AddServiceEndpoint)-所有服務創建步驟都將通過config自動執行。
我的建議:
1)刪除所有服務托管代碼,僅使用基於配置文件的配置(包括基地址,如果在* .svc中托管,則可以是相對地址,也可以是絕對地址)。 這是一種更加靈活和便捷的方式(如果您有可能使用配置文件,有時甚至沒有),我已經使用了很多年。 參見簡單的wcf配置示例 )
2)使用單調實例(更可預測)。 最終,您將只有一行代碼,例如Host = new ServiceHost(new MyService())
。
3)不要使用生成的(svcutil)wcf客戶端代碼。 只需在服務和客戶之間共享合同庫(服務合同,數據合同)即可。 然后使用ChannelFactory調用服務方法: Channel = new ChannelFactory<MyService>("bindingConfigurationName").CreateChannel()
或new ChannelFactory<MyService>(binding, serviceAddress).CreateChannel()
。 這很好,足以同步調用服務方法(通過做一些額外的工作,您甚至可以通過了解和使用簡單的同步服務接口來異步調用服務方法!)
4)不要使用WSDualHttpBinding-它不能正常工作(至少在Internet和防火牆上)。 我建議使用tcpbinding(本質上是雙工的,幾乎在任何地方都可以使用)。 但是,如果您使用WSDualHttpBinding-為什么沒有雙向方法(雙工協定,請參見示例 )?
5)使用標准客戶端(例如wcftestclient,SoapUI甚至是提琴手或郵遞員)測試您的服務(用於RESTful服務)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.