从.NET客户端连接到WCF服务时出错

时间:2010-08-19 02:15:30

标签: c# .net wcf windows-7 proxy

我使用VS 2010中的默认模板创建了一个新的WCF项目。

然后我创建了一个控制台应用程序,作为连接到WCF服务的客户端。

以下是我用来连接服务的代码:

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

CompositeType ctype = client.GetDataUsingDataContract(new CompositeType());

当代码到达尝试调用方法GetDataUsingDataContract()的位置时,我得到以下异常:

 Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section.

Server stack trace: 
   at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
   at System.Net.WebRequest.get_InternalDefaultWebProxy()
   at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
   at System.Net.HttpRequestCreator.Create(Uri Uri)
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.ServiceModel.Channels.HttpChannelFactory.GetWebRequest(EndpointAddress to, Uri via, NetworkCredential credential, TokenImpersonationLevel impersonationLevel, AuthenticationLevel authenticationLevel, SecurityTokenProviderContainer proxyTokenProvider, SecurityTokenContainer clientCertificateToken, TimeSpan timeout)
   at System.ServiceModel.Channels.HttpChannelFactory.GetWebRequest(EndpointAddress to, Uri via, SecurityTokenProviderContainer tokenProvider, SecurityTokenProviderContainer proxyTokenProvider, SecurityTokenContainer clientCertificateToken, TimeSpan timeout)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, SecurityTokenContainer clientCertificateToken, TimeoutHelper& timeoutHelper)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, TimeoutHelper& timeoutHelper)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at WcfTestClient.ServiceReference1.IService1.GetDataUsingDataContract(CompositeType composite)
   at WcfTestClient.ServiceReference1.Service1Client.GetDataUsingDataContract(CompositeType composite) in H:\My .NET Projects\WcfTest\WcfTestClient\Service References\ServiceReference1\Reference.cs:line 120
   at WcfTestClient.Program.Main(String[] args) in H:\My .NET Projects\WcfTest\WcfTestClient\Program.cs:line 15
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

客户端app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:61704/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

我知道服务和客户端没有任何问题,因为我将它复制到另一台机器并连接,称为方法,并且没有任何问题返回结果。

我的本​​地计算机上是否有某些设置阻止我连接此服务?

请帮助!!!

由于

3 个答案:

答案 0 :(得分:2)

好了,请阅读错误消息:

  

创建Web代理时出错   在...中指定   'system.net/defaultProxy'   配置部分。

那么 - 你能告诉我们你web.config中<system.net>/<defaultProxy>中定义的值吗?最有可能是有些可疑的......

答案 1 :(得分:0)

托管服务在哪里,例如它是否在IIS中的另一台机器上运行?服务是否由Casini(Visual Studio的开发Web服务器)托管运行?

如果它由IIS托管,客户端上的浏览器是否可以从服务器查看.svc文件,并且“您已经开心创建了服务消息”?

答案 2 :(得分:0)

尝试使用Service Trace Viewer来追踪您的问题所在。

生成日志并将其加载到查看器后,您会很快发现问题。问题可能是从狡猾的配置到没有正确序列化的数据对象到WCF代码中抛出的异常。