Is possible to access WCF Service without adding Service Reference?
Asked Answered
I

5

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;
}
Iguanodon answered 30/10, 2013 at 6:20 Comment(0)
D
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.

Drastic answered 30/10, 2013 at 6:56 Comment(4)
I think the third line should be ChannelFactory<ServiceContract> factory = new ChannelFactory<ServiceContract>(binding, address);Sismondi
using System.ServiceModel;Costumier
@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?Costumier
posted new question here with a little more detail: #35072025Costumier
T
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

Titivate answered 30/10, 2013 at 6:56 Comment(0)
G
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).

Gelatinoid answered 22/5, 2014 at 0:28 Comment(1)
This is assuming that both uri have the same "wsdl"... say, you have QA and PROD servers. Is that correct?Testate
D
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);
Debacle answered 22/8, 2014 at 10:38 Comment(0)
C
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.

Choanocyte answered 29/4, 2015 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.