I have two projects - a WCF service that provides operations on a database, and an ASP.NET project running AngularJS that acts as a client to the service.
I would like to combine these into a single project. That is, when running the service, the interface (the ASP.NET AngularJS project) should appear.
I have seen some sources saying that AspNetCompatibilityMode can be used to do something like this, but I haven't seen anywhere how to actually specify a client.
Is this the right way to go about doing this? Is there a simpler way? Thanks in advance!
It is possible. I assume you want to expose existing WCF service in your ASP.NET web forms/mvc (whatever) type of project.
Steps:
1) make sure your ASP.NET project references the assembly in which is the WCF service implementation
2) change in your ASP.NET project your global.asax to:
using System.ServiceModel.Activation; // from assembly System.ServiceModel.Web
protected void Application_Start(Object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.Add(new ServiceRoute("Services/Angular", new WebServiceHostFactory(), typeof(WCFNamespace.AngularService)));
}
This registers calls starting with the /Service/Angular prefix to be handled by your WCF service.
3) your WCF service should look like this
[ServiceContract]
public interface IAngularService
{
[OperationContract]
[WebGet(UriTemplate = "/Hello", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[Description("Returns hello world json object")]
HelloWorld GetHello();
}
[DataContract]
public class HelloWorld
{
[DataMember]
public string Message { get; set; }
}
Note the methods - they should be decorated with [WebGet] or [WebInvoke] methods since for Angular you want to build RESTfull wcf service. Also serialization/deserialization format is set to json.
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class AngularService : IAngularService
{
public HelloWorld GetHello()
{
return new HelloWorld { Message = "Hello from WCF. Time is: " +
DateTime.Now.ToString() };
}
}
Now you should be able to get json object if you type /Services/Angular/Hello into browser.
And finally as you've noted the WCF contract implementation (in this case class AngularService) has to be marked with attribute [AspNetCompatibilityRequirements] so the IIS can host it under ASP.NET web forms/MVC project.
Disclaimer: this is very naive implementation, in real world you probably would want to catch & log exceptions that happen in service and return them to client in form of json.
Related
We have built a WCF service for our customers and deployed the dlls. Everything works perfectly, but we have been told we have to change the URL of the WCF.
Lets say, our current WCF URL is like below
https://xxx.xxxxx.xxx/EGov/PostBox.svc
Now, they want us to change that URL to
https://xxx.xxxxx.xxx/EGov/TransferService.svc
It is easy to change the file name from PostBox.svc to TransferService.svc but we have do it for our 100 customers and that is impossible. So, we are wondering if we can do URL-REWRITE from CONFIG files.
If we can do the URL-REWRITE from config files, then we will email the config files to each customer to put it into the right folder.
I hope I made my question clear.
URL rewriting in WCF
Create a new WCF service application.
Add Global.asax file.
In Application_Start method add below lines:
RouteTable.Routes.Add(new ServiceRoute("api/Service1", new WebServiceHostFactory(), typeof(Service1)));
Add WebInvoke in Operation Contract of Service Contract (interface IService1).
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetData?value={value}")]
Build and browse Project.
Check this : http://gunaatita.com/Blog/URL-rewriting-in-WCF/1052
No sure whether this will help you but i had some similar scenario where i have to rewrite the URL(for me i didn't have to show .svc file name to users.
public class Route : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += delegate
{
//URL Rewriting
//---To remove the .svc extension service file name from URL----
HttpContext cxt = HttpContext.Current;
string path = cxt.Request.AppRelativeCurrentExecutionFilePath;
{
cxt.RewritePath(path.Insert(1, "/ReportingService.svc"), false);
}
};
}
}
Hopefully this will help you at least in some way
I have a web service and a client DLL. The web service uses an Oracle database.
For testing the client DLL, I copied the web service and made it point to a test database. Then I copied the client DLL and added this test web service using "Add web reference".
What I would like to do is to use one web service and one client DLL but be able to tell the client DLL to use either use the test or production database rather than two identical web serivces and client DLLs.
Edit
I mis-stated the issue. What I need to do is use one client DLL and two web services (one production version, one development/test version) and be able to, somehow, tell the client DLL which web services to use.
This is a sample of how the web service, client DLL and client app are used:
public class DSSService : System.Web.Services.WebService
{
public DSSService()
{
}
[WebMethod(MessageName = "GetFacility", BufferResponse=true, Description = "blah.")]
public Facility GetFacility(string sFDBID, string sZip, string sFinNo)
{
Facility oFacility = ...;
...
return oFacility;
}
....
}
Client DLL:
namespace DSSConfig
{
string sWSURL;
public class Config
{
public Config()
{
}
public void SetWSURL(string sURL)
{
sWSURL = sURL;
}
public Facility GetFacility(string sFDBID, string sZip, string sFinNo)
{
DSSService Proxy = new DSSService();
proxy.Url = sWSURL;
Facility oFacility = Proxy.GetFacility(sFDBID, sZip, sFinNo);
return oFacility;
}
In client application, having DSSConfig DLL as reference:
DSSConfig oConfig = new DSSConfig();
oConfig.SetWSURL("http://myserver/WebService1/service.asmx");
oConfig.GetFacility("blah", "blah", "blah");
What you need to do is change the WEB Service to take a parameter that it will use to construct the connection string to the DB.
Then change client DLL to pass that parameter as part of the call or connection.
Then you can configure the Client DLL to using any technique you like to pass the parameter. My suggestion is perhaps derive a class from the generated proxy in the client DLL and use this in the client code.
Without specific implementation details I can't be more precise.
I created a WCF self hosted web service. Here are my serviceContract and OperationContract in Instace class:
[ServiceContract]
public interface ISwiperWS
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getstatus?callback={Callback}", ResponseFormat = WebMessageFormat.Json)]
String getStatus(String Callback);
}
TestWS.cs
public String getStatus()
{
return "true"
}
I am accessing the endpoint of this web service from JsonP written in GWT
String url = "https://somedomain.com:8083/getstatus";
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setTimeout(600);
jsonp.setCallbackParam("callback");
jsonp.send(url);
I create a setup project and installed it on different-different machine. Whenever i am making a request to web service endpoints from JsonP It is showing an strange behavior. In some machine i am getting an expected response where as in some other machine it continuously showing an error i.e. 405-method are not allowed.
I searched it for and make change according to them but nothing works for me. Please suggest me a solutoin
May be: I believe it had something to do with your jquery ajax call using jsonp. If you are going to change it to just json the post will work..
I have the basic ASP.NET authentication domain service running in my Silverlight app (the one that comes with the VS2010 Silverlight Business template).
How can I use the authentication that this grants to secure methods exposed by standard WCF services (also hosted in the same app in IIS)?
Ok, so this is how you do it, the standard WCF service needs a couple of attributes, and you need to assign the Thread.CurrentPrincipal:
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
public Service1()
{
Thread.CurrentPrincipal = HttpContext.Current.User;
}
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "Registered Users")]
public string DoSomeWork()
{
return "working";
}
}
A good place to start is here: Security for WCF RIA Services. What you're looking for is the RequiresAuthentication attribute.
[RequiresAuthentication]
public Foo SomeMethodCall(object parameter1)
{
return service.GetResult(parameter1)
}
I have a web site which has a simple web service. I can call the web service successfully from javascript on the page. I need to be able to call the same web service from a C# forms application.
The web service code is very simple:
[WebService(Namespace = "http://myurl.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class IDCService : System.Web.Services.WebService {
public IDCService () {
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
My javascript works:
function HelloWorld() {
var yourName = $get('txtYourName').value;
alert(yourName);
IDCService.HelloWorld(HelloWorldCalback, failureCB);
}
function HelloWorldCalback(result) {
alert(result);
}
function failureCB(result) {
alert("Failed");
}
However, when I try to set a reference to the WS in my C# code what I expect to see is an object with a method "HelloWorld", what I in fact see is an object with properties like "HelloWorldRequest", "HelloWorldResponse", "HelloWorldRequestBody" and so forth.
I am new to web services, and am very confused. Any help would be appreciated.
Depends on how you added your reference :-)
If you added it by clicking "Add Web Reference", you specified the location of the service, and you gave it a namespace - let's assume it would be called "MySVC".
In that case, you should be able to do this in your Winforms program:
MySVC.MyTestService svc = new MySVC.MyTestService();
string message = svc.HelloWorld();
and thus retrieve the output of the HelloWorld method.
On the other hand, if you clicked on "Add Service Reference" (which is not the same - this will add a WCF client side proxy to your web service), then you'd get those request and response object classes. You should also get a xxxxClient class, and that's what you'll use:
MyWCFService.MyTestServiceSoapClient client =
new MyWCFService.MyTestServiceSoapClient();
string message = client.HelloWorld()
That way, you should be able to access all your methods on your web service, too.
Marc