服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 总结C#动态调用WCF接口的两种方法

总结C#动态调用WCF接口的两种方法

2021-12-07 14:27garfieldzf C#

这篇文章给大家总结了C#动态调用WCF接口的两种方法,大家可以根据自己的需求选择对应的方式,下面来一起看看。

如何使用

1、第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用。

2、使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在Invoke类里封装服务契约接口即可。

客户端调用DEMO

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//第一种方式
string url = "http://localhost:3000/DoubleService.svc";
IDoubleService proxy = WcfInvokeFactory.CreateServiceByUrl<IDoubleService>(url);
int result = proxy.Add(1, 3);
 
 
//第二种方式<br><br>int result1 = WCFInvoke.Invoke(t => t.Add(1, 3));<br><br>
<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="NewBehavior">
     <dataContractSerializer maxItemsInObjectGraph="65536000" />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <bindings>
   <basicHttpBinding> 
    <binding name="BasicHttpBinding_IDoubleService"
        closeTimeout="01:00:00"
        openTimeout="01:00:00"
        sendTimeout="01:00:00"
        receiveTimeout="01:00:00"
        maxBufferSize="2147483647"
        maxBufferPoolSize="524288"
        maxReceivedMessageSize="2147483647">
     <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    </binding>
   </basicHttpBinding>
   <netMsmqBinding>
    <binding name="NetMsmqBinding_IAsyncSender">
     <security mode="None" />
    </binding>
   </netMsmqBinding>
  </bindings>
  <client>
   
   <endpoint address="http://localhost:3000/DoubleService.svc"
        binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IDoubleService"
        contract="DoubleStone.WebHost.IDoubleService"
        name="BasicHttpBinding_IDoubleService" />
  
  </client>
 </system.serviceModel>

第一种调用方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class WcfInvokeFactory
  {
    #region WCF服务工厂
    public static T CreateServiceByUrl<T>(string url)
    {
      return CreateServiceByUrl<T>(url, "basicHttpBinding");
    }
 
    public static T CreateServiceByUrl<T>(string url, string bing)
    {
      try
      {
        if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!");
        EndpointAddress address = new EndpointAddress(url);
        Binding binding = CreateBinding(bing);
        ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
        return factory.CreateChannel();
      }
      catch (Exception ex)
      {
        throw new Exception("创建服务工厂出现异常.");
      }
    }
    #endregion
 
    #region 创建传输协议
    /// <summary>
    /// 创建传输协议
    /// </summary>
    /// <param name="binding">传输协议名称</param>
    /// <returns></returns>
    private static Binding CreateBinding(string binding)
    {
      Binding bindinginstance = null;
      if (binding.ToLower() == "basichttpbinding")
      {
        BasicHttpBinding ws = new BasicHttpBinding();
        ws.MaxBufferSize = 2147483647;
        ws.MaxBufferPoolSize = 2147483647;
        ws.MaxReceivedMessageSize = 2147483647;
        ws.ReaderQuotas.MaxStringContentLength = 2147483647;
        ws.CloseTimeout = new TimeSpan(0, 30, 0);
        ws.OpenTimeout = new TimeSpan(0, 30, 0);
        ws.ReceiveTimeout = new TimeSpan(0, 30, 0);
        ws.SendTimeout = new TimeSpan(0, 30, 0);
 
        bindinginstance = ws;
      }
      else if (binding.ToLower() == "nettcpbinding")
      {
        NetTcpBinding ws = new NetTcpBinding();
        ws.MaxReceivedMessageSize = 65535000;
        ws.Security.Mode = SecurityMode.None;
        bindinginstance = ws;
      }
      else if (binding.ToLower() == "wshttpbinding")
      {
        WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
        ws.MaxReceivedMessageSize = 65535000;
        ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
        ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
        bindinginstance = ws;
      }
      return bindinginstance;
 
    }
    #endregion
  }

第二种调用方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class WCFInvoke
  {
    /// <summary>
    /// 你需要调用的服务契约
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="func"></param>
    /// <returns></returns>
    public static T Invoke<T>(Func<IDoubleService, T> func)
    {
      IServiceInvoker serviceInvoker=new WCFServiceInvoker();
      return serviceInvoker.InvokeService(func);
    }
  }
public interface IServiceInvoker
  {
    void InvokeService<T>(Action<T> invokeHandler) where T : class;
    TReslt InvokeService<T, TReslt>(Func<T, TReslt> invokeHandler) where T : class;
  }
 
public class WCFServiceInvoker:IServiceInvoker
  {
    private static readonly ChannelFactoryManager FactoryManager = new ChannelFactoryManager();
 
    private static readonly ClientSection ClientSection =
      ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
 
 
    public void InvokeService<T>(Action<T> invokeHandler) where T : class
    {
      KeyValuePair<string, string> endpointNameAddressPair = GetEndpointNameAddressPair(typeof(T));
      var arg = FactoryManager.CreateChannel<T>(endpointNameAddressPair.Key, endpointNameAddressPair.Value);
      var obj2 = (ICommunicationObject)arg;
      try
      {
        invokeHandler(arg);
      }
      finally
      {
        try
        {
          if (obj2.State != CommunicationState.Faulted)
          {
            obj2.Close();
          }
        }
        catch
        {
          obj2.Abort();
        }
      }
    }
 
 
    public TReslt InvokeService<T, TReslt>(Func<T, TReslt> invokeHandler) where T : class
    {
      KeyValuePair<string, string> endpointNameAddressPair = GetEndpointNameAddressPair(typeof(T));
      var arg = FactoryManager.CreateChannel<T>(endpointNameAddressPair.Key, endpointNameAddressPair.Value);
      var obj2 = (ICommunicationObject)arg;
      try
      {
        return invokeHandler(arg);
      }
      finally
      {
        try
        {
          if (obj2.State != CommunicationState.Closed || obj2.State != CommunicationState.Faulted)
          {
            obj2.Close();
          }
        }
        catch
        {
          obj2.Abort();
        }
      }
    }
 
    private KeyValuePair<string, string> GetEndpointNameAddressPair(Type serviceContractType)
    {
      var configException =
        new ConfigurationErrorsException(
          string.Format(
            "No client endpoint found for type {0}. Please add the section <client><endpoint name=\"myservice\" address=\"http://address/\" binding=\"basicHttpBinding\" contract=\"{0}\"/></client> in the config file.",
            serviceContractType));
      if (((ClientSection == null) || (ClientSection.Endpoints == null)) || (ClientSection.Endpoints.Count < 1))
      {
        throw configException;
      }
      foreach (ChannelEndpointElement element in ClientSection.Endpoints)
      {
        if (element.Contract == serviceContractType.ToString())
        {
          return new KeyValuePair<string, string>(element.Name, element.Address.AbsoluteUri);
        }
      }
      throw configException;
    }
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public class ChannelFactoryManager : IDisposable
  {
    private static readonly Dictionary<Type, ChannelFactory> Factories = new Dictionary<Type, ChannelFactory>();
    private static readonly object SyncRoot = new object();
 
    public void Dispose()
    {
      Dispose(true);
    }
 
    public virtual T CreateChannel<T>() where T : class
    {
      return CreateChannel<T>("*", null);
    }
 
    public virtual T CreateChannel<T>(string endpointConfigurationName) where T : class
    {
      return CreateChannel<T>(endpointConfigurationName, null);
    }
 
    public virtual T CreateChannel<T>(string endpointConfigurationName, string endpointAddress) where T : class
    {
      T local = GetFactory<T>(endpointConfigurationName, endpointAddress).CreateChannel();
      ((IClientChannel)local).Faulted += ChannelFaulted;
      return local;
    }
 
    protected virtual ChannelFactory<T> GetFactory<T>(string endpointConfigurationName, string endpointAddress)
      where T : class
    {
      lock (SyncRoot)
      {
        ChannelFactory factory;
        if (!Factories.TryGetValue(typeof(T), out factory))
        {
          factory = CreateFactoryInstance<T>(endpointConfigurationName, endpointAddress);
          Factories.Add(typeof(T), factory);
        }
        return (factory as ChannelFactory<T>);
      }
    }
 
    private ChannelFactory CreateFactoryInstance<T>(string endpointConfigurationName, string endpointAddress)
    {
      ChannelFactory factory = null;
      factory = !string.IsNullOrEmpty(endpointAddress) ? new ChannelFactory<T>(endpointConfigurationName, new EndpointAddress(endpointAddress)) : new ChannelFactory<T>(endpointConfigurationName);
 
      factory.Faulted += FactoryFaulted;
      factory.Open();
      return factory;
    }
 
    private void ChannelFaulted(object sender, EventArgs e)
    {
      var channel = (IClientChannel)sender;
      try
      {
        channel.Close();
      }
      catch
      {
        channel.Abort();
      }
    }
 
    private void FactoryFaulted(object sender, EventArgs args)
    {
      var factory = (ChannelFactory)sender;
      try
      {
        factory.Close();
      }
      catch
      {
        factory.Abort();
      }
      Type[] genericArguments = factory.GetType().GetGenericArguments();
      if ((genericArguments.Length == 1))
      {
        Type key = genericArguments[0];
        if (Factories.ContainsKey(key))
        {
          Factories.Remove(key);
        }
      }
    }
 
    protected virtual void Dispose(bool disposing)
    {
      if (disposing)
      {
        lock (SyncRoot)
        {
          foreach (Type type in Factories.Keys)
          {
            ChannelFactory factory = Factories[type];
            try
            {
              factory.Close();
            }
            catch
            {
              factory.Abort();
            }
          }
          Factories.Clear();
        }
      }
    }
  }

总结

第一种方式比较常见,第二种方式是我参考另外一个项目中的写法,其中的有一些细节我还没有搞明白,实现了这个功能后还需要再看看这部分代码,再消化消化。以上就是这篇文章的全部内容,希望能给大家带来一定的帮助,如果有疑问大家可以留言交流。

原文链接:http://www.cnblogs.com/sword-successful/p/4760376.html

延伸 · 阅读

精彩推荐
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10