跟着Artech学习WCF(1)

简介: 折腾了老半天双向通信,不是端口绑定不上 就是创建代理失败要摸是 代理没有及时关闭Artech的代码看了半天无论是照抄还是改良都是不行,无奈到处看看test最后终于解决了 绑定协议现在只试了http还没有事tcp的 项目结构图如下   目前感觉客户端的调用部分代码很多不 来自博客园地址忘了呵呵     代码如下 using System; using System.

折腾了老半天双向通信,不是端口绑定不上 就是创建代理失败要摸是 代理没有及时关闭Artech的代码看了半天无论是照抄还是改良都是不行,无奈到处看看test最后终于解决了

绑定协议现在只试了http还没有事tcp的

项目结构图如下

2

 

目前感觉客户端的调用部分代码很多不 来自博客园地址忘了呵呵

 

 

代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Contracts
{
    [ServiceContract(Namespace="http://www.artech.com/",CallbackContract=typeof(ICallback))]
   public interface ICalculator
    {
        [OperationContract(IsOneWay = true)]
        void add(double x, double y);

        [OperationContract(IsOneWay = true)]
        void Subtract(double x, double y);


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Contracts
{
   public interface ICallback
    {
       [OperationContract(IsOneWay=true)]
       void DisplayResult(double x, double y, double result);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contracts;
using System.ServiceModel;

namespace Services
{
   public class CalculatorService:ICalculator
    {

        void ICalculator.add(double x, double y)
        {
            double result = x + y;
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            callback.DisplayResult(x, y, result);
        
        }

        void ICalculator.Subtract(double x, double y)
        {
            double result = x - y;
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            callback.DisplayResult(x, y, result);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Services;
using Contracts;

namespace Hosting
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            {
                try
                {

                    host.Opened += delegate
                    {
                        Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                    };
                    host.Open();
                    Console.Read();
                }
                catch (Exception ex)
                {

                }



            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using Contracts;


namespace Client
{
    public partial class Form1 : Form, ICallback
    {
        public Form1()
        {
            InitializeComponent();
            IniProxy();
        }

        MyDataClient client = null;

        public void IniProxy()
        {
            client = new MyDataClient(new InstanceContext(this), "WSDualHttpBinding_ICalculator");
        }

        void ICallback.DisplayResult(double x, double y, double result)
        {
            this.label1.Text = x.ToString() + "||"+y.ToString()+"||" + result.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            client.add(1, 2);

            // ServiceReference2
            #region MyRegion
            
           
            //InstanceContext instanceContext = new InstanceContext(this);
            //using (DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(instanceContext, "WSDualHttpBinding_ICalculator"))
            //{
            //    ICalculator proxy = channelFactory.CreateChannel();

            //    using (proxy as IDisposable)
            //    {
            //            proxy.add(1, 2);
            //            Console.Read();
            //    }

            //}
            #endregion


        }
    }


    class MyDataClient : ClientBase<ICalculator>, ICalculator
    {
        internal MyDataClient(InstanceContext callback, string configendpoint)
            : base(callback, configendpoint)
        {


        }

        public void add(double x, double y)
        {
            this.Channel.add(x, y);
        }

        public void Subtract(double x, double y)
        {
            this.Channel.Subtract(x, y);
        }
    }




    //class CalculateCallback : ICallback
    //{

    //    void ICallback.DisplayResult(double x, double y, double result)
    //    {
          
    //    }
    //}
}

 

服务器端的配置

  <system.serviceModel>

    <!--<behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true"  httpGetUrl="http://localhost:9999/calculatorservice/metadata"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>-->

    <!--<services>
      <service name="Services.CalculatorService">
        <endpoint
          address="http://localhost:9999/calculatorservice"
          binding=""
     
          contract="Contracts.ICalculator" />


      </service>


    </services>-->


    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true"  httpGetUrl="http://localhost:9999/CalculatorService/metadata"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
 
    <services>
      <service name="Services.CalculatorService" behaviorConfiguration="metadataBehavior">
        <endpoint address="http://localhost:9999/CalculatorService" binding="wsDualHttpBinding"
          contract="Contracts.ICalculator" />
      </service>
    </services>
  </system.serviceModel>

 

 

客户端的配置

<system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_ICalculator" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:9999/CalculatorService" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_ICalculator" contract="Contracts.ICalculator"
                name="WSDualHttpBinding_ICalculator">
                <identity>
                    <userPrincipalName value="zhangc-PC\Administrator" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
test
相关文章
|
9月前
|
网络协议 网络架构 Windows
框架学习——WCF框架
框架学习——WCF框架
229 0
|
安全 C#
WCF技术我们应该如何以正确的方式去学习掌握
一、WCF技术我该如何学习?       阿笨的回答是:作为初学者的我们,那么请跟着阿笨一起玩WCF吧,阿笨将带领大家如何以正确的姿势去掌握WCF技术。由于WCF技术知识点太多了,就纯基础概念性知识都可以单独出一本书来讲解,本次分享课程《C#面向服务编程技术WCF从入门到实战演练》开课之前,阿笨还是希望从没了解过WCF技术的童鞋们提前先了解一下WCF技术,至少要明白WCF技术的ABC三要素分别指的是什么。
1171 0
|
机器学习/深度学习 消息中间件 开发框架
跟着Artech学习WCF(4) MSMQ 绑定
曾几何时 再看大家讨论各种构架时,经常出现在各个服务器间传递数据,经常出现MSMQ, 那是看到这个心理就郁闷,怎么整天折腾ASP.NET 就没遇见过这个东西 原来这个家伙藏在WCF里面 嘿嘿这次被我发现了 首先 第一次装IIS的话 默认是没有安装msmq 所以需要自己安装的   看Art...
784 0
|
存储
跟着Artech学习WCF(3) wcf 的状态问题
开始以为是wcf的session问题 敲了一边代码发现里面没有用session存储数据 经过 自己研究才发现作者是再将wcf的状态存储问题 项目结构如下   代码如下 using System; using System.
686 0
|
安全 Windows
跟着Artech学习WCF(2) netTcpBinding 绑定
netTcpBinding 绑定真是坑爹啊 因为一直围绕着iis的缘故 很少搞这个东西,这次计划系统的学习WCF 才搞的 没搞不知道一搞晕菜 配置了半天才弄好 最晕菜的是在省城代理类时 的地址 更晕菜 这次配置OK了 保存一下 以备他日不时之需 首先netTcpBinding是服务器端的配...
750 0
|
XML 数据格式
跟着Artech学习WCF 省的看书没环境没心情尤其没有看书的环境只有上网的环境
Artech的 我的WCF之旅(1):创建一个简单的WCF程序 http://www.cnblogs.com/artech/archive/2007/02/26/656901.html   以前自己练习WCF是总是通过创建客户端代理类和实现所有东西都是放在创建WCF的项目里面,WCF的地址(A...
866 0
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0