Can you use WcfTestClient against WebApi? - asp.net-web-api

This article suggests it was possible, or in the works, with some code that suggests it can be done, but I can't figure out what code needs to happen or the WcfTestClient's uri needs to be.
Here's the code from the article that makes me think I can do it:
// Metadata routes to support $metadata and code generation in the WCF Data Service client.
configuration.Routes.MapHttpRoute(
ODataRouteNames.Metadata,
"$metadata",
new { Controller = "ODataMetadata", Action = "GetMetadata" }
);
Is this feature implemented?

No, it does not work as you intend. WCF Test Client supports talking to SOAP-based services. OData is not supported in the current version.
Granted, as #Snixtor mentioned, you could create a SOAP service using ASP.NET Web API, including support for metadata (WSDL). But I really don't know of any good reason why anyone would want to do that.

Related

Session object not available in WebAPI

I've got a webAPI that uses Entity Framework. I'm trying to cache some data in the session variable following along in this article:
https://msdn.microsoft.com/en-us/library/system.web.httpcontext.session(v=vs.110).aspx
I can't seem to do it though. The Session object isn't available.
In my controller, I try this:
Session["mappings"] = mappings;
...but it doesn't recognize what Session is.
I also try this:
HttpContext.Current.Session["mappings"] = mappings;
...and this:
Page.Session["mappings"] = mappings;
...but it doesn't know what HttpContext or Page are.
I'm including System.Web in my project references. I'm also including this in my web.config:
...just like this article says:
https://msdn.microsoft.com/en-us/library/ms178581(v=vs.110).aspx
...but to no avail.
My work colleague suggests it's because our webAPI is RESTful which means it's stateless, so no session object. However, we know there are ways around this. What I need is simply some way of persisting data in some kind of cache that will survive across several requests.
I also need something that will be available inside EF entities (not just the webAPI controller) is that's possible.
Does anyone know how to solve this problem? Thanks.
As your colleagues correctly suggested, an API is stateless, each request is separate and needs to have all the data required to complete the request.
You can add a caching layer however, but that is not going to be done via the Session object. Session makes no sense in an API.
Have a look here for some ideas: Caching Data in Web API

How to impliment MVC Authorization when older system is in place and returns a string

I am not using any of asp.net Authentication in my code. It is handled by an outside library. I get back a success or failure from the function. So all the work is done for me.sCould I get some examples of how I would implement this in MVC3. I know a little, I have had 2 weeks experience.
Thanks.
AuthFunction("UserName", "password");
You need to implement your own MembershipProvider. The ValidateUser method will use AuthFunction method from your library.
This tutorial should be good. Just skip the repository things because those are already implemented by your library. Carefully check the configuration section at the bottom of the tutorial.

Right way to consume a Web Service in WP7

I have a Web Service like ServiceA.asmx. What is the right way to consume it?
I have two ways to consume a service:
1)adding Service Refernce:
I have added Service Refernce of ServiceA.asmx ( Like in http://microsoftfeed.com/2011/part-14-how-to-consume-a-web-service-in-windows-phone-7) and i am able to call the Functions in Service like in the link i have given. If we use this way there is no need to parse the Result, Result returned in Objects(easy to use).
2)Hitting the URL and Calling asynchronously:
Here we can hit the URL, that function will call the asynchronous function that asynchronous function will return the response. But here response will be in XML here we have to parse that XML in to an Object.(not easy if any Big XML is there)
Please Guide me on this
Personally I would use the 'Add service reference' option. It's easy to use, and this option is added to Visual Studio especially for consuming web services. You can still use MVVM to build up your models/viewmodels.
I don't have the option to check it right now, but from my head the classes generated when adding the service reference also implement INotifyPropertyChanged. So you could probably use the object directly (if they are in the structure as you want to use it.) as your Model. Based on that model you can create your own ViewModel which you can bind to the UI.
To see how this works have a look at the code samples on MSDN:
Implementing the Model-View-ViewModel Pattern in a Windows Phone Application
Weather Forecast Sample

ColdFusion MVC frameworks & RESTful Service mismatch?

Most CF MVC Frameworks use the front controller pattern. Usually Search Engine Safe (SES) plugin together with URL Rewrite are used to construct friendly URLs. However, when it comes to implementing RESTful services, using a MVC framework seems like a layer of complexity added on top of another layer of complexity.
How should one tame this beast? Any nice and clean approach of supporting RESTful services with ColdFusion? Any MVC framework out there that can expose RESTful services easily?
Thanks
ColdBox has been supporting RESTful URLs for a long time now. In 3.0 you can even split the incoming HTTP verbs to execute different actions in a nice decoupled manner. read here: http://blog.coldbox.org/post.cfm/coldbox-rest-enabled-urls
You can even have HTTP method security on your event handlers very easily:
component{
this.allowedMethods = {
LIST = "GET",
SAVE = "POST,PUT"
};
}
I have been using Powernap (http://powernap.riaforge.com) to implement RESTful web services. It's not an MVC framework, but I think it could work alongside whatever framework you're currently using in your app.
I tried using PowerNap a while ago, but I felt it didn't fit well with what I was doing (building an API on top of an existing application). My solution was RESTfulCF: it's front-controller, but doesn't implement full MVC, because (as you say) that's overkill.
We're currently using RESTfulCF to power a number of (heavy use) internal systems at White Label Dating, and it's running like a dream while allowing us to continue building the rest of the application separately from the API layer, which we use to expose just the systems we need.
Funny you should ask. I am a fan of PowerNap, but I thought it could be done a little better another way, so I started my own framework last week. It's still a front-controller framework so everything is channeled through index.cfm (which is easily removed using url-rewriting), but it's built specifically for writing RESTful web services. It draws a lot of inspiration from PowerNap as well as FW/1.
It's still kind of rough, but it works. Right now I'd call it a proof of concept; but it doesn't have far to go before I call it version 1.0. I've put some information and the source on github.
Update 8/23/2010: Now officially at 1.0! :)
I use MVC as per Fowler's PageController pattern to implement REST services. One controller per resource and the controller implements a method for each of the http methods supported. i.e. GET, PUT, POST, DELETE.
Works well for me. The only area where my approach differs from the standard interpretation of MVC is that my model is really a model of the UI content. It's not a domain model. It may contain elements from the domain model, but it may also contain other content.
Quicksilver is not bad! http://quicksilver.riaforge.org/
/**
* #url /hello/{text}
* #httpMethod GET
*/
public String function saySomething(required String text) {
return "Hello " & arguments.text;
}
Actual URL:
index.cfm/hello/developer
Another option is Taffy (https://github.com/atuttle/Taffy). Add one CFC per URI template, and define a method for each HTTP method you want to support (GET, PUT, DELETE, etc).
<cfcomponent extends="taffy.core.resource" output="false"
taffy_uri="/user/{userID}/stuff/{stuffID}/property/{propertyID}">
<cffunction name="get" access="public" output="false">
...
</cffunction>
<cffunction name="post" access="public" output="false">
...
</cffunction>
<cffunction name="delete" access="public" output="false">
...
</cffunction>
</cfcomponent>

Generating urls from MVC Routes in a Management layer...

So... I have a business object/manager which is going to generate emails.
These emails will contain links to various content on the website... and therefore needs to understand about MVC routing.. or at least how to generate URLs for the website...
However my business object will not have access to a RequestContext etc and the email generation is not necessarily the result of a web request to a website (I have a dispatcher which runs on a background thread which will be generating the emails)
Any ideas how I can generate my urls without having access to a request - and therefore being unable to use URLHelper...
Thoughts?
In order to get at the UrlHelper outside of the controller, you need to feed it and the routing data the HttpContext. Here's an example:
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
UrlHelper helper = = new UrlHelper(new RequestContext(context, RouteTable.Routes.GetRouteData(context)));
I prefer to define schema and make both routing and business logic aware of it. Means different implementations of the same URL schema.
Some reasons why:
Your routing mechanism could change. For example in feature you can switch to url_rewrite module.
Possible issues with load-balanced installation.
You do not need even to try to use URLHelper in undocumented way.
BTW, you can replace HttpRequest from URLHelper easily. We used to use this for unit-testing. For more information just search for unit testing of the HttpContextBase or look at examples in source code of the MvcContrib. This can help to instantiate URL helper and all related stuff in non hosted environment. But I still do not think that this is a good idea.
In ASP.NET MVC5 (and possibly MVC4 - I'm not sure when it was introduced), you can do this more directly using HttpRequest.RequestContext. Eg:
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

Resources