2014-05-19 39 views
0

我有一個簡單的自主主機WCF服務。我有這個服務在遠程服務器上運行。我能夠與我的機器上運行的客戶端進行通信以進行服務(我是本地管理員)。 但是,當我在不同的機器上運行相同的客戶端(非管理員),他們無法溝通。WCF客戶端無法在某些機器上連接

我監控資源管理器,我看到兩個隨機本地端口在每次服務調用時都打開並回撥。所以我無法打開特定的端口。

任何想法可能的原因或其他機器上的防火牆配置更改可能是什麼?

我對WCF很新。請原諒我,如果它的基本問題。

WCF服務器代碼

namespace CService 
    { 
    class Program 
     { 
     static void Main(string[] args) { 

     Console.Title = "C Service"; 

     // Step 1 of the address configuration procedure: Create a URI to serve as the base address. 
     Uri baseAddress = new Uri("http://" + GetServerIPPort.ServerIP + ":" + GetServerIPPort.Port + "/CService/Service"); 


     // Step 2 of the hosting procedure: Create ServiceHost 
     ServiceHost selfHost = new ServiceHost(typeof(CSerice), baseAddress); 

     try 
     { 
      // Step 3 of the hosting procedure: Add a service endpoint. 
      selfHost.AddServiceEndpoint(typeof(ICService), new BasicHttpBinding(), "CService"); 

      // Step 4 of the hosting procedure: Enable metadata exchange. 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      selfHost.Description.Behaviors.Add(smb); 

      // Step 5 of the hosting procedure: Start (and then stop) the service. 
      selfHost.Open(); 
      Console.WriteLine("The Coemet Service is ready and its listening on {0}", baseAddress.AbsoluteUri.ToString() + ":" + baseAddress.Port.ToString()); 

      Console.WriteLine("Press <ENTER> to terminate service."); 
      Console.WriteLine(); 
      Console.ReadLine(); 

      // Close the ServiceHostBase to shutdown the service. 
      selfHost.Close(); 
     } 
     catch (CommunicationException ce) 
     { 
      Console.WriteLine("An exception occurred: {0}", ce.ToString()); 
      selfHost.Abort(); 
     } 
    } 
} 

我有此鏈接http://msdn.microsoft.com/en-us/library/ms733133(v=vs.110).aspx

我的客戶代碼片段看起來是這樣的幫助generatedProxy對象。

string serviceEndPointAddress = "http://" + GetServerIPPort.ServerIP + ":" +  GetServerIPPort.Port + "/CService/Service/CService"; 

      var remoteAddress = new System.ServiceModel.EndpointAddress(new Uri(serviceEndPointAddress)); 
      object rawOutput; 
      using (var client = new CServiceClient(new BasicHttpBinding(), remoteAddress)) 
      { 
       client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 100); 
       try 
       { 
        rawOutput = client.GetData(Identifier, field, date); 
       } 
       catch (Exception e) 
       { 
        errorMsg = e.ToString(); 
       } 
      }\n 

錯誤trowed在 「client.GetData(識別符,字段,日期)」

System.Runtime.Serialization.InvalidDataContractException:類型「System.Threading.Tasks.Task 1[System.Object]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types. at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAttribute dataContractFormatAttribute, MessageDescription messageDescription, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy) at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch) at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters) at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose) at System.ServiceModel.ChannelFactory.CreateFactory() at System.ServiceModel.ChannelFactory.OnOpening() at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.ChannelFactory.EnsureOpened() at System.ServiceModel.ChannelFactory 1.CreateChannel(的EndpointAddress地址,烏里經由) 在System.ServiceModel.ClientBase 1.CreateChannel() at System.ServiceModel.ClientBase 1.CreateChannelInternal() 在System.ServiceModel.ClientBase`1.get_Channel() 在CServiceClient.GetData(字符串標識符,字符串字段中,日期時間日期)

+0

問:你有什麼具體的錯誤?請發佈確切的錯誤消息(並在可能的情況下生成消息的相應代碼行)。 – FoggyDay

+0

請分享CService的實現。該服務可能需要授權。 –

+0

如果您還可以發佈您的ICService代碼,並且您的數據傳輸對象未在同一文件中定義,請發佈。 –

回答

2

在.NET 4.5,對於在WCF基於任務的異步操作的新的支持。當您使用VS 2012或更高版本在開發計算機上生成代理時 - 默認情況下可以包含這些代理。

現在,您正在使用的新機器很可能運行在.NET 4.0上,因此不知道基於任務的異步操作會發生什麼 - 因此是例外。

這是一個非常簡單的修復程序,用於支持運行.NET 4的客戶端。0,你只需要執行以下操作中服務參考設置的:

  1. 取消選中允許生成異步操作
  2. 選擇生成異步操作的代替基於任務的操作

特別感謝這個blog post

+1

爲了使用.Net 4.0生成代理,我使用了.NET 4.0 SDK的svcutil.exe並解決了問題。謝謝!! – user781700

0

根據到t他例外的是傳遞一個不可序列化的對象。我懷疑這是在下面的代碼行標識符:

rawOutput = client.GetData(Identifier, field, date); 

根據堆棧跟蹤內部CService期待兩個字符串和日期時間:

System.ServiceModel.ClientBase`1.get_Channel ()在 CServiceClient.GetData(字符串標識符,字符串字段,日期爲準)

如果你需要使用WCF,你應該使用DataContractAttribute屬性爲傳遞一個自定義對象(數據傳輸對象)類和DataMemberAttribute每個成員,像這樣:

[DataContract] 
public class Identifier 
{ 
    [DataMember] 
    public string Id {get;set;} 
} 
+0

我能夠從我的機器連接到此服務,但問題來自於另一臺機器。序列化錯誤不是特定於計算機的。 – user781700

+0

所以你暗示了這個例外並不代表潛在的問題? –

相關問題