What's the URL to test a SOAP WCF web service? - wcf

I'm very new to WCF SOAP.
Having successfully built a basic REST( web api) web service and tested it through a url (eg a GET on api/users/1 to return a user with id = 1 from database), I'm now attempting something similar with a WCF SOAP web service.
I don't want to build a proxy client. Ideally i'd like to just test within browser or using Fiddler.
Here's the ABC (Address, Binding, Contract) program.cs code:
static void Main(string[] args)
{
// Step 1 Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:1234/WCFsoap");
// Step 2 : Create ServiceHost (this will host the webservice)
ServiceHost selfHost = new ServiceHost(typeof(UserLookupWebService), baseAddress);
try
{
// Step 3 : Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(IUserLookup), //Contract
new BasicHttpBinding(), //Binding
"newapi"); //Address (relative to selfhost url)
For example:
How would I call a findUser(int userId = 1) method within a UserLookupWebService class? And can i do this from a browser or Fiddler?

You can either create a proxy class using svcutil.exe and use that class to connect to your service:
svcutil http://localhost:1234/WCFsoap
Or use any SOAP test tool out there:
I really like Soap UI but you can use as well WCF Test client for example

Related

How to pass parameter and get data from wcf service

I got this error:
There was no endpoint listening at http://vkalra.in/WCF_SERVICE/RestServiceImpl.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details
InnerException-The remote server returned an error: (404) Not Found.
This is my code:
Binding binding = new BasicHttpBinding();
// Create endpointAddress of the Service
EndpointAddress endpointAddress = new EndpointAddress("http://vkalra.in/WCF_SERVICE/RestServiceImpl.svc");
ServiceReference1.Leave_Details emp = new ServiceReference1.Leave_Details();
emp.empid = items.empid;
emp.fromdate = "01-04-2019";
emp.todate = "04-06-2019";
emp.tabt = "1";
emp.jdis = "0";
try
{
ServiceReference1.RestServiceImplClient service = new ServiceReference1.RestServiceImplClient(binding, endpointAddress);
string levbal = service.Leave_Calculation(emp);
}
catch(Exception ex)
{
}
If we want to consume the WCF service created by WebHttpBinding(this type of the WCF service is also called Restful service) by adding service reference, we need to do something special. Generally speaking, if we want to call Restful service (such as the service created by Asp.net WebAPI), we could construct a http request, Get or Post with a request body, then send to the specified service address.
https://code-maze.com/different-ways-consume-restful-api-csharp/
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
This also applies to WCF service created by WebHttpBinding.
consuming a WCF service using simple HttpClient class
But if we want to consume the WCF restful service by adding the service reference, we are supposed to maintain the consistency of binding between the client and server. we should add [WebGet]/[WebInvoke] to the auto-generated method of the service interface, which comes in the form of adding service reference, located in Reference.cs file.
One more thing we need do is add Webhttpbehavior (endpoint behavior) to the client service endpoint. This client configuration comes in the form with adding service reference, located in the System.ServiceModel section of the app.config/web.config.
The remote server returned an unexpected response: (400) Bad Request. wcf
Feel free to let me know if there is anything I can help with.

[Web API]Add Web App to WCF Service solution

I have an issue related with WCF Service and Web API with below steps.
Create a WCF Service Application which is Service1.svc
Right Click this project, and Add -> Web API -> Web API Controller Class
Could I host Web API in WCF solution like this? I did not find any place to configure or register Web API, so if I run this project, Web API will not run correctly.
Code For hosting web api
public class Service1 : IService1
{
public Service1()
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
No, you can't add a web API controller into a WCF service project and expect it to work. WCF and WebAPI have almost no overlap in terms of their dependency on the BCL.
I think Prashant, in his comment, puts it very eloquently when he argues:
why you want to do this ?
You can host both a WebAPI project and a WCF service project in the same solution, but even though you can do this, why would you want to?
If you want to expose the same endpoints over both SOAP and REST you can already do this using only WCF.

Configure WCF Service address in Xamarion iOS

In my Xamario.iOS application which I'm doing in VisualStudio, I'm consuming WCF services by adding the ServiceReferences (Add ServiceReference) and I get my services added in ServiceReference folder as below:
LoginService (This is of Login.svc)
RegisterService (This is of Register.svc)
my code looks like
LoginRequest req = new LoginRequest()
req.username = "test"
req.password = "test"
LoginResponse res = LoginService.Authenticate(req)
and I get my response which is fine.
But I would like to configure the WCF service's address dynamically, so that in future I can change my service URL just in one place rather than updating all the services under ServiceReference folder.
Right now, I don't find any config file which has the configuration details for the WCF services added, in my Xamarin.iOS project.
If I create the Service Client in code behind, I can get the client for each service, but in such case how can I have my LoginRequest and LoginResponse type?
Your client class should have a constructor overload that allows you to specify the address of the endpoint.

Duplex WCF service

I created a simple WCF service that worked just fine. I tried later to make it a duplex service.
I created the duplex contract, changed the binding to wsDualHttpBinding and pulish the WSDL. However when I try to do the last step on the client side I face some trouble. Here is what I did on the client side:
static void Main(string[] args)
{
InstanceContext context = new InstanceContext(new CalcCallbackHandler());
CalcClient proxy = new CalcClient(context);
}
The problem is that CalcClient ctor can't receive InstanceContext.
I can't understand how It's possible beacuse all the example I've seen on the web use this method to create duplex server. Any suggestions?
On the client side you will need to setup/host the call back object and expose the callback endpoint.

WCF data service hosting in Windows Form

I want to host a WCF Data Service (formerly known as ADO.NET data Service) in windows form.Is it possible? If yes,then is there any blog, which talks about it?
I know WCF can be hosted in Windows Form, but I am not sure about WCF data service, as all the examples I see, is asking to create ASP.NET web project.
-Brajesh
It is very easy to host a WCF Data Service in a WinForms application (or in my case a unit test).
// add reference to System.Data.Services
// identify your endpoint uri
Uri endpoint = new Uri("http://localhost:12345/MyDataService");
// create the data service host
DataServiceHost host = new DataServiceHost(typeof(MyDataService), new Uri[] { endpoint });