C# 是否可以在不添加服务引用的情况下访问 WCF 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19675194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:35:46  来源:igfitidea点击:

Is possible to access WCF Service without adding Service Reference?

c#asp.netwcf

提问by User

I need to access Wcf service methods without adding Service Reference?how to do this?

我需要在不添加服务引用的情况下访问 Wcf 服务方法?怎么做?

Step 1:I create a WCF Service.
Step 2:Add Service Reference to my application.
Step 3:And Access the WCF Service methods into app.

第 1 步:我创建一个 WCF 服务。
第 2 步:将服务引用添加到我的应用程序。
第 3 步:并将 WCF 服务方法访问到应用程序中。

like this way,

像这样,

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;
}

采纳答案by Thilina H

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 hereyou can get fully workout regarding on that.

这里你可以得到充分的锻炼。

回答by wizzardz

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

是的,可以在不添加服务引用的情况下作为 WCF 服务调用。

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

第 2 步:创建 WCF 服务并将其托管在 IIS 中

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

第 3 步:在客户端应用程序中引用您的服务合同库,然后执行此代码

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

希望这可以帮助

回答by SteveCav

Risking the markdown lynch mob with this, but...

用这个冒着降价私刑暴民的风险,但是......

If the reasonyou're not adding the reference is because you need to choose the URL at runtime, you can still add the reference and then change it when you need to with:

如果您不添加引用的原因是因为您需要在运行时选择 URL,您仍然可以添加引用,然后在需要时更改它:

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

(or do the same thing in the constructor when you instantiate).

(或者在实例化时在构造函数中做同样的事情)。

回答by Leon Pro

I don`t have a reputation for commenting the answer of "Thilina H", but you can use code

我没有评论“Thilina H”的答案的声誉,但您可以使用代码

ServiceContract channel = factory.CreateChannel();

only if you wrote:

只有当你写道:

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

instead

反而

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

回答by Steve Wood

When in John did this: "Step 2:Add Service Reference to my application." Visual Studio added the endpoint and default bindings to the app.config file of his application. He does not need to specify a URL. John's code should work just fine as long as the service has implemented the required contracts.

在 John 中执行此操作时:“第 2 步:将服务引用添加到我的应用程序。” Visual Studio 将终结点和默认绑定添加到其应用程序的 app.config 文件中。他不需要指定 URL。只要服务实现了所需的契约,John 的代码就应该可以正常工作。