20

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;
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
User
  • 1,644
  • 10
  • 40
  • 64

5 Answers5

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.

Thilina H
  • 5,754
  • 6
  • 26
  • 56
  • I think the third line should be ChannelFactory factory = new ChannelFactory(binding, address); – olorin Dec 22 '15 at 09:54
  • 1
    using System.ServiceModel; – JustBeingHelpful Jan 28 '16 at 19:54
  • @Thilina H, in .NET 4.5, I'm getting red squiggly lines under "ServiceContract". I fixed based on 1st comment, but still getting error. It says "The type or namespace 'ServiceContract' could not be found" ... how do I reference the ServiceContract correctly to avoid that compile error? What needs to be done in this project and/or the WCF Service project? – JustBeingHelpful Jan 28 '16 at 20:14
  • posted new question here with a little more detail: http://stackoverflow.com/questions/35071525/consume-wcf-service-from-client-without-assembly-reference – JustBeingHelpful Jan 28 '16 at 20:37
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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
wizzardz
  • 5,664
  • 5
  • 44
  • 66
2

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).

SteveCav
  • 6,649
  • 1
  • 50
  • 52
0

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);
Leon Pro
  • 102
  • 9
0

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.

Steve Wood
  • 51
  • 5