背景:前面说了自己参加了一个BS的项目,从一开始就有一个目的,两个解决方案到底是怎么相互调用的。从最初的好奇,到探究,到询问,到最后的自己一点初探。下面就分享下自己初探的结果。
WCF服务的创建和启动
第一步:创建服务
[ServiceContract] public interface IHello //提供远程服务的接口
{
[OperationContract]
string sayHello(string name);
[OperationContract]
int sayint(int a,int b);
}
public class Hello:IHello //实现服务接口
{
public string sayHello(string name)
{
return name + "您好!";
//throw new NotImplementedException();没有实现该接口。
}
public int sayint(int a,int b)
{
return a + b;
}
}
第二步,配置终结点,启动服务
static void Main(string[] args)
{
//NetTcpBinding方式启动wcf服务
ServiceHost m_ServiceHost = new ServiceHost(typeof(Hello));//Service1是wcf服务的类名称
BasicHttpBinding binding = new BasicHttpBinding(); //传输协议
Uri baseAddress = new Uri(string.Format("http://localhost:8000/HelloServiceDemo2")); //服务基地址
m_ServiceHost.AddServiceEndpoint(typeof(HelloServiceDemo2.IHello), binding, baseAddress); 配置终结点
//BasicHttpBinding方式启动wcf服务 //下面的 代码不懂耶,大神多多指教
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = m_ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:8000/HelloServiceDemo2"));
m_ServiceHost.Description.Behaviors.Add(metadataBehavior);
}
m_ServiceHost.Open(); //启动服务
Console.WriteLine("服务已经启动");
Console.ReadLine();
}
客户端引用服务
第一步,创建一个解决方案,下载WCF服务。
步骤:创建好一个解决方案后,然后右击引用,选择添加服务引用,就到下面的界面,填入基地址,即可下载服务。
点击确定即可。
第二步,调用服务的方法
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebApplication2 .ServiceReference2.HelloClient hello = new WebApplication2 .ServiceReference2 .HelloClient(); //这里要注意HelloClient是在该解决方案下的服务名的空间下。
string a = hello.sayHello("武刚鹏");
Response.Write(a);
int sum = hello.sayint(4, 2);
Response.Write(sum);
}
}
遇到问题
一开始我是在配置文件里写好终结点,然后启动服务
配置文件的内容 <system.serviceModel>
<services>
<service name="MyHost.Hello">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/HelloServiceDemo2"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="HelloServiceDemo2.IHello">
</endpoint>
</service>
</services>
</system.serviceModel>
ServiceHost host = new ServiceHost(typeof (Hello )); //宿主程序启动服务
host.Open();
Console.WriteLine("服务已经启动");
Console.ReadLine();
然后下载服务就出问题了
载“http://localhost:8000/HelloServiceDemo2/_vti_bin/ListData.svc/$metadata”时出错。
请求失败,HTTP 状态为 400: Bad Request。
元数据包含无法解析的引用:“http://localhost:8000/HelloServiceDemo2”。
服务 http://localhost:8000/HelloServiceDemo2 不支持内容类型 application/soap+xml; charset=utf-8。客户端和服务绑定可能不匹配。
远程服务器返回错误: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。
请求失败,HTTP 状态为 400: Bad Request。
元数据包含无法解析的引用:“http://localhost:8000/HelloServiceDemo2”。
服务 http://localhost:8000/HelloServiceDemo2 不支持内容类型 application/soap+xml; charset=utf-8。客户端和服务绑定可能不匹配。
远程服务器返回错误: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。
到现在还是对WCF懵懵的。对这些错误根本没有一点头绪。求大神相助啊。