這是我建的測試服務調用另一個服務Windows窗體應用程序調用WCF服務和WCF服務調用另一個WCF服務
這是得到由其他服務調用的服務的示例項目
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」的默認端點元素。這可能是因爲沒有找到適用於您的應用程序的配置文件,或者因爲在客戶端元素中找不到匹配此合同的端點元素。
System.InvalidOperationException:(故障詳細等於一個ExceptionDetail,可能是由IncludeExceptionDetailInFaults =真,其價值是創造找不到默認終結點元素引用合同「Proxy2.IStudent」在ServiceModel客戶端配置節。這可能是因爲沒有爲您的應用程序找到配置文件,或者因爲在客戶端元素中找不到匹配此合同的端點元素。 )
at System.ServiceModel.ChannelFactory.ApplyConfigurati上(字符串configurationName,配置結構)
在System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串configurationName)
在System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串configurationName,的EndpointAddress地址)
在System.ServiceModel.ChannelFactory`1。 。(字符串endpointConfigurationName,EndpointAddress remoteAddress)
at System.ServiceModel.Configu ...)。
我需要幫助,請
哪一個是拋出異常?客戶? – Prajwal
是的。客戶端無法找到Proxy2.IStudent服務合同,但客戶端沒有撥打該服務的主服務。 –