2011-09-15 2 views
0

私が作成したWindowsサービス(特定の機能を実行するためにプラグインを使用するC#です)へのプラグインを構築しています。この新しいプラグインはWebサービスを使用してWebサービスを呼び出し、情報を取得します。C#でWebサービスのエンドポイントをプログラムで決定する

残念ながら、WebサービスのURLはDEV、QC、およびPRODUCTION環境で異なります。私はそのエンドポイントURLを構成可能にしたいと思っています(プラグインはデータベースを調べURLを取得します)。

動的エンドポイントを使用できるように、コードでどのようにWebサービス呼び出し側を設定するのですか?

私はサービスを追加してDEVの既存のものを指し示すことができます - そしてそれは私のプロキシクラスを構築しますが、URLを "ハードロック"しないようにするにはどうすればいいですか? (データベース内のURLに基​​づいてそれが引き出されます)?私は、コードの中でそれを変えることができるようにしたいと思います。

答えて

3

基本的に、あなたのWCFサービスのクライアントを作成するには、これを呼び出すことができます。

MyClient = new MyWCFClient(new BasicHttpBinding("CustomBinding"), new EndpointAddress(GetEndpointFromDatabase())); 

GetEndpointFromDatabase()stringを返しどこ - エンドポイントを。

0

URIを.configファイルに入れることはできませんか? .debug.configと.release.configの中に異なるURIを持つことによって、URIがデバッグまたはリリースされているときに変更できます。

+0

[はい]を設定し、それは、展開中の余分なステップがあることを意味します。こうすることで、エンジニアリングに "このDLLをこのフォルダにドロップする"ように指示するだけで、リスクが少なく、手順が少なくなります。 – Jason

1

LINQPadで動作するエンドツーエンドサンプルを作成しました。これは、完全に自己ホストされたシナリオ用であり、さまざまなバインディングなどを調べることができます(クライアントとサーバーの両方)。後で役立つ他の側面のいずれかが見つかった場合には、それはトップにはなく、サンプル全体を投稿してください。


void Main() 
{ 
    MainService(); 
} 
// Client 
void MainClient() 
{ 
    ChannelFactory cf = new ChannelFactory(new WebHttpBinding(), "http://localhost:8000"); 
    cf.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
    IService channel = cf.CreateChannel(); 
    Console.WriteLine(channel.GetMessage("Get")); 
    Console.WriteLine(channel.PostMessage("Post")); 
    Console.Read(); 
} 
// Service 
void MainService() 
{ 
    WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8080")); 
    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService),new WebHttpBinding(), ""); 
    ServiceDebugBehavior stp = host.Description.Behaviors.Find(); 
    stp.HttpHelpPageEnabled = false; 
    stp.IncludeExceptionDetailInFaults = true; 
    host.Open(); 
    Console.WriteLine("Service is up and running"); 
    Console.ReadLine(); 
    host.Close();  
} 
// IService.cs 
[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    [WebInvoke(Method="GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    string GetMessage(string inputMessage); 

    [OperationContract] 
    [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    string PostMessage(string inputMessage); 

    [OperationContract] 
    [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    System.IO.Stream PostJson(System.IO.Stream json); 
} 
// Service.cs 
public class Service : IService 
{ 
    public string GetMessage(string inputMessage){ 
     Console.WriteLine(inputMessage); 
     return "Calling Get for you " + inputMessage; 
    } 
    public string PostMessage(string inputMessage){ 
     Console.WriteLine(inputMessage); 
     return "Calling Post for you " + inputMessage; 
    } 
    public System.IO.Stream PostJson (System.IO.Stream json) { 
     Console.WriteLine(new System.IO.StreamReader(json).ReadToEnd()); 
     return json; 
    } 
} 
0

は、URLだけポイント

TheWebservice.Url = TheUrlFromTheDatabase; 
+0

.URLプロパティはありません。これはWCFサービス参照です。 – Jason

関連する問題