[Web API]Add Web App to WCF Service solution - wcf

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.

Related

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

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

Convert service from WCF to ServiceStack Framework

I have developed one WCF application, and it is working as a middle layer between the database and my web application. Now my client wants to transfer this WCF to REST-based using ServiceStack.
I have looked around it on GitHub and tried to build a demo. I have created a start up template using NuGet, so it includes a Hello & Todo example.
How can I transfer my logic as object based (DTO)? Because most of the functions I have with different parameters and return the result as a dataset.
How can I make a client in C#? And which reference do I need to add?
When I hosted sample application on IIS after adding the startup template using NuGet, I could not able to find any resources. Is there a specific setting I need to do when I need to host it on IIS?
If you haven't already done so go through the Creating REST Services with ServiceStack presentation.
1) If you've seen ServiceStack's Hello World example it shows you that the only steps needed to do to create a web service is to just provide:
//1. A Request DTO
public class Hello : IReturn<HelloResponse> {
public string Name { get; set; }
}
//2. A Response DTO
public class HelloResponse {
public string Result { get; set; }
}
//3. The web service implementation that takes a Request DTO and returns a Response DTO
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
The above example shows all the code needed to create the Hello web service.
You should be able to re-use a lot of your existing type and logic from your WCF method and just copy it in the Any() method.
2) One of the benefits of ServiceStack is that you don't need to add a ServiceReference, i.e. you can re-use the same generic Service Client and your DTOs for all your web services. e.g:
//Using JSON:
IServiceClient client = new JsonServiceClient("http://localhost/path/to/servicestack");
//Using XML:
IServiceClient client = new XmlServiceClient("http://localhost/path/to/servicestack");
var response = client.Send(new Hello { Name = "Arun" });
Console.WriteLine("Received: " + response.Result);
On the /metadata page there is also a link to your webservices WSDL where you could create generated service clients should you wish. However this is not the recommended approach since it requires much more friction then just using your existing DTOs.
3) ServiceStack Web Services are already an ASP.NET application, i.e. ServiceStack is just a set of IHttpHandler's you can configure to run inside of a normal ASP.NET or MVC web application by adding a Web.config mapping to your web applications Web.config.
Basically you can treat a ServiceStack web service as a normal ASP.NET web application, in fact the Hello World Tutorial shows you how to do this from creating an empty ASP.NET application.
You may also be interested in checking out The Starter Templates example projects which shows you the minimum about of setup required to configure ServiceStack to run in a variety of different hosting options, i.e. ASP.NET / Windows Service / Console Application, etc.

Programmatically creating a client proxy for a WIF-secured WCF Service

Here's what I've done so far:
1) Created an ASP.NET MVC relying party application and secured it with ADFS v2.0. This works.
2) Created a WCF Service using the Claims-Aware service template for an ASP.NET website. I've turned ASP.NET compatibility for the service ON because the service wouldn't activate otherwise. I've moved the interface for said service to a 'SharedContracts' assembly.
3) Set up the WCF service as a relying party using the "Add STS" reference, also pointing at my ADFS server.
4) Configured the ADFS server to include the WCF service as a relying party and issue it LDAP claims.
What I want to do now is talk to the service using ActAs. In other words, when someone hits HomeController.Index() from the ASP.NET MVC site with a token full of claims (remember the MVC site is a relying party), I want this method to programmatically create a client proxy and invoke the single service method I have on the WCF service (a method called "HelloClaim", which is nearly identical to the stock method that comes with the claims-aware service template).
Here's the code I've got so far:
[ValidateInput(false)]
public ActionResult Index()
{
SecurityToken callerToken = null;
IClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as IClaimsPrincipal;
if (claimsPrincipal != null)
{
foreach (IClaimsIdentity claimsIdentity in claimsPrincipal.Identities)
{
if (claimsIdentity.BootstrapToken is SamlSecurityToken)
{
callerToken = claimsIdentity.BootstrapToken;
break;
}
}
string baseAddress = "http://khoffman2/SecureServices/Service.svc";
ChannelFactory<IHelloClaim> factory = new ChannelFactory<IHelloClaim>(new WebHttpBinding(), new EndpointAddress(baseAddress));
factory.ConfigureChannelFactory<IHelloClaim>();
IHelloClaim hello = factory.CreateChannelActingAs<IHelloClaim>(callerToken);
string result = hello.HelloClaim();
ViewData["Message"] = "Welcome to ASP.NET MVC!";
}
return View();
}
When I attempt to invoke the method, I get the following error message:
Manual addressing is enabled on this factory, so all messages sent must be pre-addressed.
I'm pretty sure I'm just not doing enough to configure the binding and the endpoint programmatically. If any of you have done this before or you know how to do it, I would love to be able to get this working.
Bottom line is I'm just making use of the basic identity delegation scenario - the only difference is I'm not using generated client proxies.
Take a look at this guide over at TechNet as it has a walkthrough on how to setup the scenario you've described:
http://technet.microsoft.com/en-us/library/adfs2-identity-delegation-step-by-step-guide(WS.10).aspx
In their example, I believe they are using standard WebForms, but in the case of MVC you can put the ChannelFactory initialization within the Global.asax within the Application_Start.

WCF Service Application Question

I am trying to create a WCF application that will be hosted in a Windows Service
I have two options in Visual Web Developer
1) New Website -> WCF Service
2) New Project -> WCF Service Application
Which will be suitable for creating service that can be hosted in windows service? Is there any tutorial explaining hosting in windows service using any of the above mentioned methods?
Thanks
Lijo
In most cases what you'll actually want to do is create two projects; first a WCF Service Library (that's under the WCF templates) and then a Windows Service (which is under the Windows templates), then reference the WCF Service Library from your Windows Service.
The WCF Service Library holds all of your WCF-specific classes - contracts, services, etc. - then you just create a ServiceHost inside the Windows service. This last part requires very little code:
public class MyService : ServiceBase // Windows Service class
{
private ServiceHost host;
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(MyWcfLibrary.MyWcfService));
host.Open();
}
protected override void OnStop()
{
host.Close();
}
}
There's a more detailed tutorial here.
As for creating your service - if you already plan to use a NT Service as your host, just use a class library and then add a WCF service to that. Neither of the two options given will give you something suitable for using in a NT Service.
For hosting, see:
MSDN: Host a WCF Service in a managed application
Hosting WCF Services
Lijo,
This page in the MSDN help shows you how to create the WCF service by starting with a Console application. I was able to get everything done with this as a starting point.

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