2013-11-04 16 views
11

私は、次のコードを持っている:BasicHttpBindingをプログラムで作成する方法

[ServiceContract] 
public interface IMyInterface 
{ 
    [OperationContract] 
    Result WsFunction1 (string param); 

    [OperationContract] 
    Result WsFunction2 (string param); 

    [OperationContract] 
    Result WsFunction3 (string param); 
} 

そして、それはこのようなものを返します:

[DataContract] 
public class Result 
{ 
    string a = ""; 
    string b = ""; 

    [DataMember] 
    public string A 
    { 
     get { return a; } 
     set { a = value; } 
    } 

    [DataMember] 
    public string B 
    { 
     get { return b; } 
     set { b = value; } 
    } 
} 
"IMyInterfaceという" とは、例えば..私のWSを実装インタフェースです

BasicHttpBinding binding = new BasicHttpBinding(); 

Uri baseAddress = new Uri ("URL.svc"); 

EndpointAddress endpointAddress = new EndpointAddress (baseAddress); 

var myChannelFactory = new ChannelFactory<IMyInterface> (binding, endpointAddress); 

IMyInterface client = null; 

try 
{ 
    client = myChannelFactory.CreateChannel(); 
    var a = client.WsFunction ("XXXXXX");      
    ((ICommunicationObject)client).Close(); 
} 
catch 
{ 
    if (client != null) 
    { 
     ((ICommunicationObject)client).Abort(); 
    } 
} 

このコードを実行すると、WSに到達できますが、決して結果を入力することはできません。

私は間違っていますか?

ありがとうございます!

+0

これはコンパイルされません。文字列型はほとんどありません。big型はbool型です。 –

+0

名前を変更したからです...実際にはコンパイルされWSがメッセージを受け取っていますが、結果は得られません。 – Crasher

+0

結果の名前空間はprobaleの犯人ですが、 httpデバッガー。 –

答えて

8

BasicHttpBinding経由でサービスにアクセスする最も簡単な方法は、SilverlightユーティリティアプリケーションであるSlSvcUtil.exeからクライアントコードを生成することです。

SLsvcUtil.exe /directory:C:\users\me\Desktop http://URL.svc 

これは、生成するファイル内にMyInterfaceClientクラスを作成する必要があります。

次に、あなたのコードの中であなたが行うことができます:

var binding = new BasicHttpBinding() { 
    Name = "BindingName", 
    MaxBufferSize = 2147483647, 
    MaxReceivedMessageSize = 2147483647 
}; 

var endpoint = new EndpointAddress("URL.svc"); 

MyInterfaceClient client = new MyInterfaceClient(binding, endpoint); 

client.WSFunctionCompleted += (object sender, WSFunctionCompletedEventArgs e) => { 
    //access e.Result here 
}; 

client.WSFunctionAsync("XXXXXX"); 
あなたの走行距離は異なる場合があり

。これが動作するかどうか私に教えてください。

+1

SvcUtilを利用すると、同様の結果が得られます。しかし、私は、全体の問題は "手で"行うことだと思う。 –

+0

私は少なくとも、彼にそれを理解するために読むコードを与えるでしょう。私はなぜ彼がSvcUtilなしでそれを行う必要があるかわからない。 –

+0

それは私が必要なものだと思います!ありがとうミリー!! – Crasher

1
var binding = new BasicHttpBinding(); 
     binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyAddress, proxyPort)); 
     binding.UseDefaultWebProxy = false; 
     binding.Security.Mode = BasicHttpSecurityMode.Transport; 
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 
     binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; 

     var endpoint = new EndpointAddress("serviceadress"); 

     var authenticationClient = new WOKMWSAuthenticateClient(binding, endpoint); 
     authenticationClient.ClientCredentials.UserName.UserName = username; 
     authenticationClient.ClientCredentials.UserName.Password = password; 

このコードをローカルで実行する場合は、このコードを実行する必要があります。

ServicePointManager.Expect100Continue = false; 
関連する問題