WCF——两个解决方案引用WCF服务

    背景:前面说了自己参加了一个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'.。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。


到现在还是对WCF懵懵的。对这些错误根本没有一点头绪。求大神相助啊。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值