Is possible to access WCF Service without adding Service Reference?

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

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.

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

Answers


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.


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


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

If the reason you'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:

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

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


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

ServiceContract channel = factory.CreateChannel();

only if you wrote:

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

instead

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

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.


Need Your Help

Using png function not working when called within a function

r plot ggplot2

I have a function that does stuff and then plots based on a condition: