不写代理类也不用vs.net生成的代理类 执行时WCF动态调用(不需用到WCF接口)
在国外网站上看到代码string serviceWsdlUri = "http://127.0.0.1:6061/Service1.svc?wsdl";
DynamicProxyFactory factory = new DynamicProxyFactory(serviceWsdlUri);
int count = 0; List<object> myEndpoints = new List<object>(); foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
Console.WriteLine("Service Endpoint[{0}]", count);
Console.WriteLine("\tAddress = " + endpoint.Address);
Console.WriteLine("\tContract = " + endpoint.Contract.Name);
Console.WriteLine("\tBinding = " + endpoint.Binding.Name);
myEndpoints.Add(endpoint.Contract.Name);
}
foreach (string endpoint in myEndpoints)
{
DynamicProxy dp = factory.CreateProxy(endpoint); OperationDescriptionCollection operations = factory.GetEndpoint(endpoint).Contract.Operations; Type proxyType = dp.ProxyType;
MethodInfo[] mi = proxyType.GetMethods(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < mi.Length; i++)
{
string name = mi[i].Name;
if (operations.FirstOrDefault(x => x.Name == name) != null)
{
string returnType = mi[i].ReturnType.ToString();
Console.Write("Func: " + returnType + " " + name + "(");
ParameterInfo[] pi = mi[i].GetParameters();
for (int j = 0; j < pi.Length; j++)
{
string param = pi[j].ParameterType.FullName + " " + pi[j].Name;
Console.Write((j > 0 ? "," : "") + param);
}
Console.WriteLine(")");
}
}
dp.Close();
}
Console.ReadKey();
}可是DynamicProxyFactory 这个类一直没法找到 VS2010也没自带。 请教大哥们。。
在国外网站上看到代码string serviceWsdlUri = "http://127.0.0.1:6061/Service1.svc?wsdl";
DynamicProxyFactory factory = new DynamicProxyFactory(serviceWsdlUri);
int count = 0; List<object> myEndpoints = new List<object>(); foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
Console.WriteLine("Service Endpoint[{0}]", count);
Console.WriteLine("\tAddress = " + endpoint.Address);
Console.WriteLine("\tContract = " + endpoint.Contract.Name);
Console.WriteLine("\tBinding = " + endpoint.Binding.Name);
myEndpoints.Add(endpoint.Contract.Name);
}
foreach (string endpoint in myEndpoints)
{
DynamicProxy dp = factory.CreateProxy(endpoint); OperationDescriptionCollection operations = factory.GetEndpoint(endpoint).Contract.Operations; Type proxyType = dp.ProxyType;
MethodInfo[] mi = proxyType.GetMethods(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < mi.Length; i++)
{
string name = mi[i].Name;
if (operations.FirstOrDefault(x => x.Name == name) != null)
{
string returnType = mi[i].ReturnType.ToString();
Console.Write("Func: " + returnType + " " + name + "(");
ParameterInfo[] pi = mi[i].GetParameters();
for (int j = 0; j < pi.Length; j++)
{
string param = pi[j].ParameterType.FullName + " " + pi[j].Name;
Console.Write((j > 0 ? "," : "") + param);
}
Console.WriteLine(")");
}
}
dp.Close();
}
Console.ReadKey();
}可是DynamicProxyFactory 这个类一直没法找到 VS2010也没自带。 请教大哥们。。
解决方案 »
- System.Diagnostics.Process.Start如何进入阻塞状态,等待返回?
- 关于WindowsApplication的菜鸟问题。
- 如果直接改写计算机内存地址???
- 怎样把XML文件导入到SQL某张表中?急··在线等··
- 请大家提供关于用C#开发DCOM的例子、链接等
- MDI窗体中的一个小问题
- 请问如何得到当前DataGrid(C#)中有多少行记录!谢谢
- 求助一正则表达式
- 我的问题怎么就没人答呢,我的分散了两次都散尽了恳求大哥们帮一下!!!!!
- 请教大神,想用窗体对另外的一个仿真软件进行简单的控制该怎么做?
- 急:光纤收发器、光电转换器、光猫和光端机的区别
- C#中引用第三方控件Visio Viewer
if (serviceUrl.EndsWith("/"))
{
serviceUrl += "mex";
}
MetadataExchangeClient mexClient = new MetadataExchangeClient(new EndpointAddress(serviceUrl));
mexClient.ResolveMetadataReferences = true;
MetadataSet metaDocs = mexClient.GetMetadata();
WsdlImporter importer = new WsdlImporter(metaDocs);
ServiceContractGenerator contractGenerator = new ServiceContractGenerator();
Collection<ContractDescription> contracts = importer.ImportAllContracts();
foreach (ServiceEndpoint endPoint in importer.ImportAllEndpoints())
{
_endPoints.Add(endPoint);//把所有的endpoint收集起来,以后用
}
foreach (ContractDescription contract in contracts)
{
contractGenerator.GenerateServiceContractType(contract);
}
if (contractGenerator.Errors.Count != 0)
{
return "There were errors during code compilation.";
}
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
CodeGeneratorOptions options = new CodeGeneratorOptions();
IndentedTextWriter textWriter = new IndentedTextWriter(new StreamWriter(proxyClass));
provider.GenerateCodeFromCompileUnit(contractGenerator.TargetCompileUnit, textWriter, options);
textWriter.Close();
CompilerParameters compileoptions = new CompilerParameters();
compileoptions.ReferencedAssemblies.Add("System.dll");
compileoptions.CompilerOptions = "/lib:\"" + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
+ "\\Reference Assemblies\\Microsoft\\Framework\\v3.0\" ";//如果是在visual studio 2008里用需要加上这一句
compileoptions.OutputAssembly = proxyAssembly;
compileoptions.IncludeDebugInformation = false;
compileoptions.GenerateInMemory = false;
compileoptions.GenerateExecutable = false; CompilerResults results = provider.CompileAssemblyFromDom(compileoptions, contractGenerator.TargetCompileUnit);
例子吧
public static object ExecuteMethod(string pUrl,string pMethodName, params object[] pParams)
{
string serviceWsdlUri = pUrl; DynamicProxyFactory factory = new DynamicProxyFactory(serviceWsdlUri);
int count = 0;
List<object> myEndpoints = new List<object>(); foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
myEndpoints.Add(endpoint.Contract.Name);
}
foreach (string endpoint in myEndpoints)
{
DynamicProxy dp = factory.CreateProxy(endpoint);
OperationDescriptionCollection operations = factory.GetEndpoint(endpoint).Contract.Operations;
Type proxyType = dp.ProxyType;
MethodInfo[] mi = proxyType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < mi.Length; i++)
{
string name = mi[i].Name;
if(name==pMethodName)
{
DynamicProxy proxy = factory.CreateProxy(endpoint);
return proxy.CallMethod(pMethodName, pParams);
}
}
dp.Close();
}
return null;
}