标签:
需求:做这个项目之前需要用到一个java的webservice接口,首先想到的第一种方法就是添加服务引用了.但是这样的话会有一个config文件 生成的dll必须要读取config文件,config文件放到同一目录也不行的,需要放到另一个工具的节点中才能被另外一个工具读取,不幸的是另外一个工具不能添加节点 否者会boom
二:接下来就是只能手写地址写进去程序里面.然后在百度搜索了一下文章.代码不出意外的粘贴复制,然后自己写个c#的webserive测试完全没问题..发现给java用时候就不行了.百思不得其解,报错找不到命名空间之类的,那就不折腾了.
三:wcf提供的吧. 首先需要一个工厂方法和一个代理类.代理类可以用wcf工具生成,具体步骤如下http://blog.163.com/ningbao911@126/blog/static/3743053820132133616282/引用别人写的吧 照着做就行这是原地址.这个时候就拿到了一个代理类,然后找一个工厂的方法
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.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; }
然后下面就是使用的代码了
string url = "这里填写你的接口地址";
客户端生成的接口名 例如public interface AA就这样写
AA a=WcfInvokeFactory.CreateServiceByUrl<AA>(url);
a.方法(new 代理类里面需要调用传参的方法("参数"));
标签:
原文地址:http://www.cnblogs.com/DLhello/p/5730019.html