I need to create a backing service for a jQuery token input field control.
Our application consists of controls for a (third-party) DotNetNuke module called SimpleWrapper. The way this module works is that it provides a lightweight but not very flexible way of displaying regular ASP.NET user controls on a DNN page. The caveat is these are .ascx controls, not .aspx pages.
I'm mostly at a loss at which of the various technologies available to use. I looked at ASMX services but those mostly seem tailored to producing generated JavaScript proxy code. I need to be able to:
mount the service at a static URL
have it accept a single string parameter
have it produce JSON in a specific, but very simple format
I don't really need strong integration with ASP.NET, like being able to respond to a postback or some such. I'd also prefer something deployable just by adding a file, without having to edit configuration files. What would be a straightforward way to spit out a chunk of JSON in such an environment?
WCF (I think starting with version 3.51) has a nice "zero config" feature that integrates easily with IIS. All you have to do is
create a JSON aware interface & service
create a simple .SVC file in the IIS site.
You don't need to mess with funky .config files :-)
Example .SVC file:
<%# ServiceHost
Service="MyNamespace.MyService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Example interface & service implementation, something like this:
public class MyService : IMyService
{
public string Test(string text)
{
return text; // whatever
}
}
[ServiceContractAttribute(Namespace="http://schemas.myservice.com")]
public interface IMyService
{
[OperationContractAttribute]
[WebInvokeAttribute(UriTemplate="Test", // change this accordingly
ResponseFormat=WebMessageFormat.Json, // change this accordingly
RequestFormat=WebMessageFormat.Json, // change this accordingly
BodyStyle=Wrapped)]
string Test(string text);
}
Here is an extra cool link about all this: WCF Web Services The Easy Way.
JSON Exposed thru Restful Service
This Link will guide you step by step on how to do what I believe to be what you are looking for has actual code sample
I don't see why WCF Web services or ASMX Web Services are not suitable for what you say you need. Personally, that's the way I'd go (choosing WCF over ASMX).
I looked at the link DJ KRAZE posted and it uses an HTTP Handler plus uses some third party Javascript serializer (one extra dependency that isn't really needed since you have JavascriptSerializer if needed).
Related
I would like to ask a question.
Recently, I've learned how to make jQuery use C# methods via consuming from an ASP.NET Web service. I am thinking of exposing some database access methods such as retrieving a list of records from the database for rendering using a jQuery library.
I have on top of my head is that I create a separate project in my solution, which is a Web service project to be able to expose the said data access methods (located on a separate project also in the solution). The web service will act as interaction between my jQuery and my data access methods.
I am visualizing my idea like this:
My question is that, is my idea a good thing to do, and if not, how do you properly expose C# data access objects for use with jQuery?
Thanks!
EDIT: The Web UI is an ASP.NET Web Forms, specifically version 2.0. I'm doing this in preparation to my next job.
Yes, this is the correct approach. Typically, the Web Service is a REST API (http://en.wikipedia.org/wiki/Representational_state_transfer) that returns JSON/JSONP. This allows the client (JQuery) to use AJAX, async calls to the server.
Web API 2 (http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api) is an easy way to expose an REST API in c#.
As Richard has already explain
i will extend it and suggest you to use AngularJs instead of simple of calling jquery ajax.
Why AngularJS?
HTML is great for declaring static documents, but it falters when we
try to use it for declaring dynamic views in web-applications.
AngularJS lets you extend HTML vocabulary for your application. The
resulting environment is extraordinarily expressive, readable, and
quick to develop.
Case Studies for example
We're setting up a bunch of json web services in ASP.NET which is served as .ashx (custom handlers) files. An example would be:
/mobile/json.ashx
We'd like to implement some form of versioning as well as to not break apps which has not upgraded. So we led down this path:
/mobile/json.ashx?v=1.0
Now, of course we have have a switch statement in our custom handlers to manage the differences between api version but this doesn't sound like a very maintainable solution to me.
What are the best practises for this kind of set up and what options are available for version control?
Thanks
Placing the version in the query parameters (that is, after the ?) suggests to the user that each endpoint is individually versioned. I would avoid this.
If your web service is structured such that there are larger logical units that are being individually versioned, then I would go with something like this:
/api1/1.0/some/endpoint
/api1/1.1/some/endpoint
/api2/1.0/some/other/endpoint
/api2/2.0/some/other/endpoint
...
The version portion of the path comes directly after the thing which is being versioned. This suggests to the user that everything underneath /api1/1.1/ is version 1.1 of API 1 and everything underneath /api2/2.0/ is version 2.0 of API 2.
If someone entirely omits the version portion of the path, the latest version should be implied. So /api2/some/other/endpoint would map to, say, /api2/2.0/some/other/endpoint.
If you're using ASP.NET MVC, all of this can be accomplished very easy using route configuration in the RegisterRoutes method in Global.asax.cs. For example:
routes.MapRoute("api1/1.1", "api1/1.1/some/endpoint",
new { controller = "Api1_1_1", action = "SomeEndpoint" });
where you have a controller class Api1_1_1 with method SomeEndpoint.
We have a homegrown framework that might be useful
to implement REST based webservices.
It is a .net c# project, used in a webapplication.
What it is used like: inline substitution of template 'tags' with dynamic content. sample tag: {{recentposts window=7 max=10}}
What it does: parsing 'tag' to command with (checked) parameters, invoking a
handler configured to handle the command and return data, transforming the data with xsl,
substitute {{...}} with the result.
I have a hunch that this could be reworked to create some form of REST based
services, parsing an url to a command with parameters, invoking a handler etc.
and writing the result to http response.
As an alternative to reworking I'm looking for smth
that might be useable instead, out of the box.
What are mature (open source) frameworks that could be used?
It has to provide a http facade, to do the REST stuff easily, and besides provide an API,
a way to bypass this facade, allowing command objects to be created, having all the invocation and transformation done and instead of writing to http response to some stream.
How about ServiceStack?
Quote from the webpage:
A modern, code-first, DTO-driven, WCF replacement web services framework encouraging best-practices for creating DRY, high-perfomance, scalable REST web services
...and an "overview" slideshow.
I use EasyHttp to work with REST base serices, it works easily with JSON and XML services and also supports working with retrieved object as a dynamic object. Very easy to plug and use and you don't have to worry about Http Request/Response anymore.
I think it may be worth taking a look at OpenRasta
https://github.com/openrasta/openrasta-stable/wiki
The OpenRasta project is a web framework that les you build web
applications as simple as
public class Home { public string Get() {
return "Hello world"; } }
It's really nice to use and easy to get started with
I've currently written code to use the ServiceContractGenerator to generate web service client code based on a wsdl, and then compile it into an assembly in memory using the code dom. I'm then using reflection to set up the binding, endpoint, service values/types, and then ultimately invoke the web service method based on xml configuration that can be altered at run time.
This all currently works fine. However, the problem I'm currently running into, is that I'm hitting several exotic web services that require lots of custom binding/security settings. This is forcing me to add more and more configuration into my custom xml configurations, as well as the corresponding updates to my code to interpret and set those binding/security settings in code.
Ultimately, this makes adding these 'exotic' services slower, and I can see myself eventually reimplementing the 'system.serviceModel' section of the web or app.config file, which is never a good thing.
My question is, and this is where my lack of experience .net and C# shows, is there a way to define the configuration normally found in the web.config or app.config 'system.serviceModel' section somewhere else, and at run time supply this to configuration to the web service client?
Is there a way to attach an app.config directly to an assembly as a resource or any other way to supply this configuration to the client?
Basically, I'd like attach an app.config only containing a 'system.serviceModel' to the assembly containing a web service client so that it can use its configuration. This way I wouldn't need to handle every configuration under the sun, I could let .net do it for me.
Fyi, it's not an option for me to put the configuration for every service in the app.config for the running application.
Any help would be greatly appreciated.
Thanks in advance!
Bryan
Create a custom class deriving from
ChannelFactory.
Override the protected
CreateDescription method. In the
override, you need to...
Call base.CreateDescription().
Read in your custom configuration.
Create a custom ServiceEndpoint based
on your configuration. Don't forget
the bindings, behaviors, etc.
Return that custom ServiceEndpoint.
More details HERE
The following couple links talk about loading WCF Configuration settings from config files other than the app.config. May be what you are looking for but not certain.
http://blogs.msdn.com/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx
http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx
Are your proxy classes deriving from ClientBase<T>? If so, then there is a constructor that accepts a Binding and an EndpointAddress. You should be able to use those instead of the corresponding configuration data.
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I want to do what a SoapExtension can do if you provide a custom SoapExtensionAttribute... but to do it for every SOAP call without having to add the extension attribute to dozens of WebMethods.
What I'm looking for is any extension point that lets me hook in as:
void ProcessMessage(SoapMessage message)
without needing to individually decorate each WebMethod. It's even fine if I have to only annotate the WebServices - I only have a few of those.
There is a configuration property, soapExtensionTypes that does this, but affects all web services covered by the .config (all of them in the same directory or a sub-directory as the .config)