2011-11-18 48 views
0

多麼奇怪。幾個月前,我編寫了一個WCF服務,我使用自簽名證書通過安全的Net.Tcp連接在本地機器上進行了測試;一切正常。然後我使用真正的SSL證書部署到雲服務器,並且一切仍然正常。爲什麼我的自簽名證書無效?

但現在,我需要再次在我的本地盒子上調試一些代碼,並且發生了一些變化,使得我的本地WCF連接不再工作! (不,證書尚未過期!)約2分鐘後,我得到了EndpointNotFoundException

Could not connect to net.tcp://mypc:8000/. The connection attempt lasted for a 
time span of 00:02:06.5152363. TCP error code 10060: A connection attempt failed 
because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond 
192.168.241.1:8000 

下面是一個創建客戶端連接的代碼:

protected override IWebService CreateChannel() { 
    var baseAddress = CommunicationManager.DebugMode ? SharedConstants.WEB_SERVER_TEST_URL : SharedConstants.WEB_SERVER_LIVE_URL; 
    var factory = new DuplexChannelFactory<IWebService>(new InstanceContext(SiteServer.Instance)); 
    factory.Endpoint.Address = new EndpointAddress("net.tcp://{0}:{1}/".Fmt(baseAddress, SharedConstants.PORT_WEB_SERVICE)); 
    var binding = new NetTcpBinding(SecurityMode.Message); 
    binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
    binding.SendTimeout = TimeSpan.FromMinutes(10); 
    factory.Endpoint.Binding = binding; 
    // to get past a strange error, do this: 
    // (see http://geekswithblogs.net/RandyMichak/archive/2009/03/04/programattically-setting-the-maxitemsinobjectgraph-property-in-client.aspx) 
    foreach (var op in factory.Endpoint.Contract.Operations) { 
    var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>(); 
    dataContractBehavior.NullOr(b => b.MaxItemsInObjectGraph = 2147483647); 
    } 

    var cred = SiteServer.GetCredentials(); 
    if (cred != null) { 
    var u = factory.Credentials.UserName; 
    u.UserName = cred.CustomerID.ToString(); 
    u.Password = cred.Password; 
    } 
    return factory.CreateChannel(); 
} 

和這裏的配置服務器端:

<service name="MyProgram.WebService.WebService" behaviorConfiguration="mex"> 
    <endpoint address="net.tcp://localhost:8000" binding="netTcpBinding" contract="MyProgram.IWebService" bindingConfiguration="SecureBinding" name="MyEndPoint"></endpoint> 
    <endpoint address="mex" binding="mexTcpBinding" name="MEX" contract="IMetadataExchange"/> 
    <host> 
    <baseAddresses> 
     <add baseAddress="net.tcp://localhost:8000"/> 
    </baseAddresses> 
    </host> 
</service> 
... 
<netTcpBinding> 
    <binding name="SecureBinding" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" portSharingEnabled="false" 
      maxBufferSize="2147483647" > 
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> 
    <security mode="Message"> 
     <message clientCredentialType="UserName"/> 
    </security> 
    </binding> 
</netTcpBinding> 
+2

您是否檢查過eventlog以查看是否有任何有用的信息? –

+0

@RussellTroywest:在事件日誌中沒有用處,但我確實給了它更長的時間來連接,並且捕獲了'EndpointNotFoundException'。看到我的編輯... –

回答

1

Doh!

自上次調試會話以來,我已經更新了我的防病毒/防火牆軟件,並且新的防火牆阻止了我! 剛剛打開了端口,一切正常。 有多令人尷尬...:-o

2

你一直在試圖接受調試模式中的所有證書,這樣的:

#if DEBUG 

ServicePointManager.ServerCertificateValidationCallback = 
    new RemoteCertificateValidationCallback(delegate{ return true; }); 

#endif 
+1

這是一個解決方案還是可能的問題?如果解決方案,應該在哪裏插入代碼? –

+0

@Shaul,我發現你已經找到了解決方案:-),但是如果有人需要這個解決方案,並且這個代碼應該放在首次請求Web服務之前。 –

+0

事實上,無論如何,爲你+1這個答案...感謝您的幫助! :) –