Using MVC routing engine from a self hosted dll (outside asp.net)? - asp.net

I'm using WCF Web API to create a self-hosted/InProcess REST Service (HttpServiceHost) that the client app will use. All the examples for the web api use ASP.Net routing engine. Would it be possible to use the routing engine outside of Asp.net?
To give you an idea what i'm doing, here is the contructor of my Service Class that the client will new up:
Public Sub New()
ObjectFactory.Initialize(Sub(x)
x.For(Of IIssueTrackerRepository)().Use(Of IssueTrackerRepository)().Ctor(Of String).Is(ConfigurationManager.ConnectionStrings("Dev").ConnectionString)
'x.ForConcreteType(Of IssueTrackerResource)().Configure.Ctor(Of String).Is(ConfigurationManager.ConnectionStrings("Dev").ConnectionString)
End Sub
)
_host = New HttpServiceHost(ObjectFactory.GetInstance(Of IssueTrackerResource), "http://localhost:8000")
_host.Open()
End Sub
If you can link any examples, it would be extremely helpful.

As far as I know: not yet.
But as Web API is still under development it may be part of a future Preview.

Related

API versioning in .NET application

I need some clarification on API versioning in .Net Core framework.
My client want the version to be handled in Router level. Like
[Route("1/[controller]")]
public class SampleController : Controller
{
[HttpGet("version")]
public IActionResult GetVersion()
{
return Ok({"Message": "API Version 1"});
}
}
I access this using, https://www.somedomain.com/api/1/sample/version
In IIS, I will create an application called 'api' (The path 'api' in my URL will be taken care here) under default web site and host my code here.
In order to do API versioning, what is the better way that I can follow here.
Can I do this?
[ApiVersion("1")]
[Route("{version:apiVersion}/[controller]")]
public class SampleController : Controller
{
[HttpGet("version")]
public IActionResult GetVersion()
{
return Ok({"Message": "API Version 1"});
}
[HttpGet("version"), MapToApiVersion("2" )]
public IActionResult GetVersion()
{
return Ok({"Message": "API Version 2"});
}
}
Is it possible to create an application under an application in IIS. Like,
Default Web Site - > api -> 1 -> Code without API version mentioned
Default Web Site - > api -> 2 -> Updated Code without API version mentioned
Or can I create the versions as application in IIS and deploy the code under each applciation version. Like,
Default Web Site - > 1 -> Code without API version mentioned
Default Web Site - > 2 -> Updated Code without API version mentioned
This will end up in changing my API URL, which i don't prefer. I still want to go with the same URI.
I access this using, https://www.somedomain.com/api/1/sample/version
Please advise the best approach that I can follow here.
Here is a popular repository that provides a set of libraries for adding API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core applications.
For ASP.NET Core applications, you can install this repository's ASP.NET Core API Versioning by running the following command in the Package Manager Console:
Install-Package Microsoft.AspNetCore.Mvc.Versioning
Perhaps the Map extension method of ApplicationBuilder suits your needs :
app.Map( "/1", myVersion1MappingFunction)
in the Configure method of Startup let myVersion1MappingFunction configure a separate middleware pipeline:
private static void myVersion1MappingFunction( IApplicationBuilder app)
{
// start your special middleware for version 1
app.UseMvc( routes =>
{
routes.MapRoute( ... );
}
}
On using Map extension the fragment ("/1") is removed from HttpRequest.Path
If I understand you correctly are wanting to use URL Path Segment Versioning for ASP.NET Core. With that said in your examples you will NOT have separate website deployed. You have one website deployed and you do NOT create multiple applications for versioning under your default website.
With URL path segment versioning you have one web application and that application manages all routes using the ApiVersion convention. You will need to maintain the code in such a way that it can deliver old functionality with new functionality and manage all dependencies.
I would recommend reading what Microsoft has to say about this here and doing a simple proof of concept that makes sense for your implementation.
I hope this helps clear up your confusion about deploying the application multiple times for versioning.
In your case the best method would be to employ the versioning from the web server level so you can have different deployments and a folder per version without specifying a version in the application routing itself. (your option 2/3?)
However since IIS merely proxies requests to kestrel with .net core unlike asp.net, you'll have to setup the reverse proxy by URL/URL Re-write with ARR to different versions of the deployment.
So you could have:
/root/V1/
/root/V2/
etc... like you explain.
Each deployment would be running kestrel with different ports numbers and IIS would re-verse proxy to them by URL.
Here is an article on how to setup ARR with url-write. it's written with asp.net in mind, but it's the same principal:
Reverse Proxy with URL Rewrite v2 and Application Request Routing

how to call webapi function from codebehind in webforms

I added a web api in my asp.net web forms project. I want to call this web api directly from my code behind. Both my code behind and web api are in the same project. Both are in same namespaces.
Is it the right way to instantiate the controller class and directly call the method in it(in webforms project - in button click). like:
SampleController obj = new SampleController();
Dictionary<String,String> values = obj.MyMethod();
Please suggest an alternative way, if this method is incorrect.
(This web-api is also consumed by another windows service using httpclient)
Thanks in advance.

Using WCF http web service in ASP.NET application

We have web application written in ASP.NET webforms with some asmx web services. Now we want to add some toher web services, which will serve different purpose than the old ones. We decided to use WCF framework.
My colleague created a new project in our solution, where he implemented the web service. Unfortunatelly he did not use the WCF* project templates, but normal console application with the following method for starting the WS:
public static void StartWS() {
if (_selfHost!=null)
StopWS();
Uri baseAdress = new Uri(WSIntegrationService.ServerUrl);
_selfHost = new ServiceHost(typeof(WSIntegrationService), baseAdress);
_selfHost.AddServiceEndpoint(typeof(IWSIntegrationService), new WSHttpBinding(SecurityMode.None), WSIntegrationService.EndPointName);
_selfHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
_selfHost.Open();
logger.Info("Integration WS started on adress " + baseAdress);
}
Now I have to integrate the project to our web application (another project in solution).
What is the easiest way to do it?
How can I ensure that both - the old asmx service as well as the new WCF service is operational?
In the end the solution was not that hard:
In web app project choose Add New Item -> WCF Service. This should create Service.svc, Service.svc.cs and IService.cs. It also modifies your web config. Now you can try if it works, with the auto generated method DoWork. Simply use something like SoapUI app to do that. ALso you can try to browse to the svc file and check the wsdl (e.g.http://localhost:2753/WSIntegration/Service1.svc?wsdl)
Delete the files IService.cs and Service.csv.cs. Modify the Service.svc file, so it contains just the reference to the class implementing the WS, e.g. like this:
That's all. I haven't deploy the solution to the IIS yet, but I don't expect any problems there. As soon as I will do that. I will update the unswer if any additional steps will be needed.

What is the advantage of using web API over web method in ASP.NET

I am familiar with web method. Now I got a suggestion to use web API instead of web method. I had done a demo of ASP.NET web API it's more closer to a MVC architecture am using the classical asp.net web development. I don't like to mess up the controller (MVC concept) with classical development.
My web Method :
[WebMethod]
public static string GetName(int id)
{
return "testName";
}
My Web API controller:
public class MyController : ApiController
{
[HttpGet]
public string GetName(int id)
{
return "testName";
}
}
am really confused on this issue any one have a better idea on the same.
What is your suggestion on the same which is the better option?
How can i compare, if both having same piece of code?
The classic ASP.NET WebServices (what you call WebMethod) are a deprecated technology. There is no longer any active development. The ASP.NET Web API is a complete rewrite of the web stack from Microsoft in which you have far greater control for creating RESTful web services. This doesn't mean that you should choose between one or the other. There's also ServiceStack. If you are starting a new project you should stay away from classic webservices. If they are still present in the .NET framework it is for compatibility reasons with legacy code.
Complementing Darin's answer, if you want to test your method from ApiController, you can inject the object's dependencies using an DI container (http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver). The dependency injection is done automatically.
However, with webmethods, you can't use DI in that way because webmethods must be static. If you insist in using DI, you need to instantiate and call the container directly in each of the webmethods to get the dependencies to work on.

How to retrieve data using Web services in .NET

How to retrieve data from database using Web services on VB?
Does it have to be a web service? Have you considered WCF? (Windows Communication Foundation)
http://msdn.microsoft.com/en-us/netframework/aa663324.aspx
If I understand you correctly, you are asking how to setup a web service using vb.net to communicate with the database and return the results.
If that is correct, then there are 2 parts:
How to write a web service
How to communicate with a database
Here are three pages that will help you create a web service:
vbdotnetheaven
codeguru
techrepublic
As for the database, if you are talking about SQL Server, then see this page here and this page here
Your question isn't clear enough. Are you trying to access a web service run by ASP.NET using VB? If yes, you can make SOAP request to your web service easily using VB, your web service will then fetch the data from database and return to you. You can have a look at this tutorial on how to make a SOAP request using VB -
http://www.aspfree.com/c/a/VB.NET/Calling-a-Web-Service-using-VB6-with-SOAP-30/1/
Hope that helps.
Create a proxy class from the web service's WSDL, using Visual Studio's "Add Web Reference" feature
Configure the proxy, such as for security, if needed (the details depend on the API)
Call the proxy from your code

Resources