2011-06-15 11 views
0

2つのWebサービスがローカルにホストされていて、両方が同じ機能を持つとします。 同じソリューションでは、これらのWebサービスをテストするコンソールアプリケーションがあります。WCF .net Webservicesメソッドを動的に呼び出す

//SERVICE 1 
namespace Service1 
{ 

[ServiceContract] 
public interface IServiceAuthentication 
{ 
    [OperationContract] 
    string Authenticate(Authentication aut); 
} 


[DataContract] 
public class Authentication 
{ 
    [DataMember] 
    public string username; 
    [DataMember] 
    public string password; 
} 

public class AuthenticationService : IServiceAuthentication 
{ 
    public string Authenticate(Authentication aut) 
    { 
     if (aut.username == "Test1" && aut.password == "Test1") 
     { 
      return "passed"; 
     } 
     else 
     { 
      return "failed"; 
     } 
    } 
} 
} 

それらの両方に同じ機能 - JUST別の資格情報CONSOLEのアプリには、両方のWEBSERVIES

namespace WebserviceTest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     WSHttpBinding wsHttpBinding = new WSHttpBinding(); 
     EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8080/Service1/Authentication"); 
     IServiceAuthentication authService1 = new ChannelFactory<IServiceAuthentication>(wsHttpBinding, endpointAddress).CreateChannel(); 
     Console.Write(authService1.Authenticate(new Authentication() { username = "test1", password = "test1" })); 
     Console.ReadKey(); 

     EndpointAddress endpointAddress2 = new EndpointAddress("http://localhost:8081/Service2/Authentication"); 
     IServiceAuthentication authService2 = new ChannelFactory<IServiceAuthentication>(wsHttpBinding, endpointAddress2).CreateChannel(); 
     Console.Write(authService2.Authenticate(new Authentication() { username = "test2", password = "test2" })); 
     Console.ReadKey(); 

    } 
} 

[ServiceContract] 
public interface IServiceAuthentication 
{ 
    [OperationContract] 
    string Authenticate(Authentication aut); 
} 


[DataContract] 
public class Authentication 
{ 
    [DataMember] 
    public string username; 
    [DataMember] 
    public string password; 
} 

} 

をテスト

//SERVICE 2 
namespace Service1 
{ 

[ServiceContract] 
public interface IServiceAuthentication 
{ 
    [OperationContract] 
    string Authenticate(Authentication aut); 
} 


[DataContract] 
public class Authentication 
{ 
    [DataMember] 
    public string username; 
    [DataMember] 
    public string password; 
} 

public class AuthenticationService : IServiceAuthentication 
{ 
    public string Authenticate(Authentication aut) 
    { 
     if (aut.username == "Test2" && aut.password == "Test2") 
     { 
      return "passed"; 
     } 
     else 
     { 
      return "failed"; 
     } 
    } 
} 
} 

私が遭遇しています問題は、ということですメソッドはWebサービス側から正しく実行されていますが、オブジェクトパラメータAu thenticationはnullです - 私は "test1"、test1 "と" test2 "、" test2 "を渡しています。どちらの呼び出しでも、サービスは失敗しています。

+0

契約を3つのプロジェクトにコピーしただけで、これらの契約が同じであることを正しく理解していることを正しく理解していますか? –

+0

はい - Webサービスを初めて利用しています - 何が最善の方法ですか? –

答えて

3

このようにしたい場合は、別のアセンブリで契約を定義し、3つのプロジェクトすべてから契約を参照します。

各アセンブリで契約を再度作成する場合は、既定の名前を使用します。デフォルトの命名規則では、シリアル化されたデータの名前空間を定義するための規約がいくつか使用されていますが、契約の.NET名前空間に基づいています。したがって、一度契約がシリアル化されると、同じデータを転送するように見えますが、その観点からデータが間違った名前空間に定義されるため、サービスはそれをスキップします。

編集:あなたは複数のアセンブリにコピーしてデータコントラクトを持つようにしたい場合は

手動[DataContract(Namespace = "SomeUri")]を自分の名前空間を定義し、すべての定義上で同じ値を使用する必要があります。

+0

私は別のアセンブリで契約を分けました。私はコンソールテストアプリでリファレンスを追加しました。今、どのようにWebサービスプロジェクトに同じ参照を追加できますか? –

+0

サービスプロジェクトのコンテキストメニューから参照を追加します。 –

+0

これは完璧に機能してくれてありがとう! –

関連する問題