20

サービス参照を追加せずに Wcf サービス メソッドにアクセスする必要がありますか?どうすればよいですか?

ステップ 1:WCF サービスを作成します。
ステップ 2: アプリケーションにサービス参照を追加します。
ステップ 3:WCF サービス メソッドにアクセスしてアプリにアクセスします。

このように、

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
protected void Button1_Click(object sender, EventArgs e)
{
    UserDetails userInfo = new UserDetails();
    userInfo.UserName = TextBoxUserName.Text;
    userInfo.Password = TextBoxPassword.Text;
    userInfo.Country = TextBoxCountry.Text;
    userInfo.Email = TextBoxEmail.Text;
    string result = obj.InsertUserDetails(userInfo);
    LabelMessage.Text = result;
}
4

5 に答える 5

23

You can use as follows. Just make sure to add the Service contract reference.

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:4684/Service1.svc");
ChannelFactory factory = new ChannelFactory<ServiceContract>(binding, address);
ServiceContract channel = factory.CreateChannel();
string resturnmessage = channel.YourMethod("test");

From here you can get fully workout regarding on that.

于 2013-10-30T06:56:43.097 に答える
3

Yes, it is possible to invoke as WCF service without adding a service reference.

As a first step, I assume that you have your service contact interface as a separate class library.

Step 2: Create your WCF service and host it in IIS

Step 3: Refer your service contract library in the client application and then follow this code

ChannelFactory<IYourServiceContract> factory = new ChannelFactory<IYourServiceContract>("EndpointNameOfYourService");
factory.Endpoint.Address = new EndpointAddress("http://example.com/service");  

IYourServiceContract client = factory.CreateChannel();
var result = client.YourMethodtoInvoke(serviceArguments);

Hope this helps

于 2013-10-30T06:56:41.377 に答える
2

これでマークダウンリンチ暴徒を危険にさらすが...

参照を追加しない理由が、実行時に URL を選択する必要があるためである場合でも、参照を追加して、必要に応じて変更することができます。

MyProxy.Endpoint.Address = new EndpointAddress(MyUri);

(または、インスタンス化するときにコンストラクターで同じことを行います)。

于 2014-05-22T00:28:53.193 に答える
0

「Thilina H」の回答にコメントするという評判はありませんが、コードを使用できます

ServiceContract channel = factory.CreateChannel();

あなたが書いた場合のみ:

var factory = new ChannelFactory<ServiceContract>(binding, address);

代わりは

ChannelFactoryfactory = new ChannelFactory<ServiceContract>(binding, address);
于 2014-08-22T10:38:03.167 に答える