Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过反射在WCF中设置超时_C#_Wcf_Reflection_Timeout - Fatal编程技术网

C# 通过反射在WCF中设置超时

C# 通过反射在WCF中设置超时,c#,wcf,reflection,timeout,C#,Wcf,Reflection,Timeout,我需要在使用WCF时设置超时 异常:“System.NullReferenceException”类型的异常,变量propertyInfo为null。我无法获取代理类型的属性超时 我有下面的代码,设置值时出错。(I硬编码“1000”) private object GetWCFInstance(ref CompilerResults CompilerResults,string WCFAddress,string contractName) { 对象proxyInstance=null; //定

我需要在使用WCF时设置超时

异常:“System.NullReferenceException”类型的异常,变量propertyInfo为null。我无法获取代理类型的属性超时

我有下面的代码,设置值时出错。(I硬编码“1000”)

private object GetWCFInstance(ref CompilerResults CompilerResults,string WCFAddress,string contractName)
{
对象proxyInstance=null;
//定义WSDL的Get地址、契约名称和参数,我们可以随时提取WSDL的详细信息
Uri地址=新Uri(WCFAddress);
//对于HttpGet端点,使用mexMode为.HttpGet的服务WSDL地址;对于MEX端点,使用MEX地址和mexMode为.MetadataExchange
MetadataExchangeClientMode mexMode=MetadataExchangeClientMode.HttpGet;
//字符串contractName=“IService1”;
//从服务获取元数据文件。
MetadataExchangeClient MetadataExchangeClient=新的MetadataExchangeClient(地址,mexMode);
metadataExchangeClient.ResolveMetadataReferences=true;
//如果服务需要,还可以通过以下两行帮助提供凭据。
//ICredentials networkcredentials=新的networkCredential(“,”,”);
//metadataExchangeClient.HttpCredentials=网络凭据;
//获取服务的元数据信息。
MetadataSet MetadataSet=metadataExchangeClient.GetMetadata();
//导入所有合同和端点。
WsdlImporter WsdlImporter=新的WsdlImporter(元数据集);
//导入所有合同。
收款合同=wsdlImporter.ImportAllContracts();
//导入所有端点。
ServiceEndpointCollection allEndpoints=wsdlImporter.ImportAllEndpoints();
//为每个合同生成类型信息。
ServiceContractGenerator ServiceContractGenerator=新ServiceContractGenerator();
//dictionary已定义为保持所有合同端点存在,合同名称是字典项的键。
var endpointsForContracts=新字典();
foreach(合同中的合同描述)
{
serviceContractGenerator.GenerateServiceContractType(合同);
//保留每个合同端点的列表。
endpointsForContracts[contract.Name]=allEndpoints.Where(ep=>ep.contract.Name==contract.Name).ToList();
}
//为合同生成代码文件。
CodeGeneratorOptions CodeGeneratorOptions=新的CodeGeneratorOptions();
codeGeneratorOptions.BracingStyle=“C”;
//创建指定语言的编译器实例。
CodeDomProvider CodeDomProvider=CodeDomProvider.CreateProvider(“C#”);
//添加与WCF相关的程序集引用作为复制器参数,以便编译特定的服务契约。
CompilerParameters CompilerParameters=新的编译器参数(新字符串[]{“System.dll”、“System.ServiceModel.dll”、“System.Runtime.Serialization.dll”});
compilerParameters.GenerateInMemory=true;
//获取已编译的程序集。
compilerResults=codeDomProvider.CompileAsemblyFromDOM(编译器参数,serviceContractGenerator.TargetCompileUnit);
if(compilerResults.Errors.Count t.IsClass&&t.GetInterface(contractName)!=null&&
t、 GetInterface(typeof(ICommunicationObject.Name)!=null);
//现在我们得到了特定契约的第一个服务端点。
ServiceEndpoint ServiceEndpoint=endpointsForContracts[contractName].First();
//通过将端点绑定和地址作为参数传递来创建代理实例。
proxyInstance=compilerResults.CompiledAssembly.CreateInstance(proxyType.Name,false,System.Reflection.BindingFlags.CreateInstance,null,
新对象[]{serviceEndpoint.Binding,serviceEndpoint.Address},System.Globalization.CultureInfo.CurrentCulture,null);
System.Reflection.PropertyInfo PropertyInfo=proxyType.GetProperty(“超时”);
**propertyInfo.SetValue(proxyInstance,1000,null)**
对象检查=propertyInfo.GetValue(proxyInstance,null);
}
返回代理实例;
}                

谢谢

最后我可以解决这个问题,我不需要app.config中的任何东西,wef.config使用相同的代码配置,并将这行代码放入客户端

我只需要强制转换ChannelFactory,然后强制转换BasicHttpBinding并设置参数

谢谢大家

            var timeOut=new TimeSpan(0, 10, 0);
            int timeOutInt = 2147483647;
            PropertyInfo channelFactoryProperty = proxyInstance.GetType().GetProperty("ChannelFactory");

        if (channelFactoryProperty == null)
        {
            throw new InvalidOperationException("There is no ''ChannelFactory'' property on the DomainClient.");
        }
        ChannelFactory factory = (ChannelFactory)channelFactoryProperty.GetValue(proxyInstance, null);
        factory.Endpoint.Binding.SendTimeout = timeOut;
        factory.Endpoint.Binding.OpenTimeout =  timeOut;
        factory.Endpoint.Binding.ReceiveTimeout = timeOut;
        factory.Endpoint.Binding.CloseTimeout = timeOut;

        BasicHttpBinding _binding = (BasicHttpBinding)factory.Endpoint.Binding;
        _binding.MaxBufferPoolSize = timeOutInt;
        _binding.MaxBufferSize = timeOutInt;
        _binding.MaxReceivedMessageSize = timeOutInt;
        _binding.OpenTimeout = timeOut;
            var timeOut=new TimeSpan(0, 10, 0);
            int timeOutInt = 2147483647;
            PropertyInfo channelFactoryProperty = proxyInstance.GetType().GetProperty("ChannelFactory");

        if (channelFactoryProperty == null)
        {
            throw new InvalidOperationException("There is no ''ChannelFactory'' property on the DomainClient.");
        }
        ChannelFactory factory = (ChannelFactory)channelFactoryProperty.GetValue(proxyInstance, null);
        factory.Endpoint.Binding.SendTimeout = timeOut;
        factory.Endpoint.Binding.OpenTimeout =  timeOut;
        factory.Endpoint.Binding.ReceiveTimeout = timeOut;
        factory.Endpoint.Binding.CloseTimeout = timeOut;

        BasicHttpBinding _binding = (BasicHttpBinding)factory.Endpoint.Binding;
        _binding.MaxBufferPoolSize = timeOutInt;
        _binding.MaxBufferSize = timeOutInt;
        _binding.MaxReceivedMessageSize = timeOutInt;
        _binding.OpenTimeout = timeOut;