How to add a reference to my Web Service Proxy from a Custom Class - asp.net

I'm creating a custom class to abstract out some of the repeated SOAP header work. I want to reference a Web Service Reference I just created in my custom class so I can create an instance of it. How do I reference it?
Notice I said I am trying to reference a Web Service "reference" (right click in VS and I added a "Web Service Reference" not a "Web Service"). So I'm trying to create an instance of that Proxy class that was created in MyCustomClass.cs

Once you have the reference created you need to add an import(vb) or using(c#) statement in the code file you want to use it. After that you simply need to instantiate an instance of the web service class.
// add the service reference
using ServiceReference1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create the instance of the web sevice of the class
SomeWebService sweb = new SomeWebService();
//call the web services HelloWorld Method
sweb.HelloWorld();
}
}
Hopefully this was what you were asking for

This is more Web Site weirdness. I recommend that web sites be used only for pages, images, css, js, etc, Anything else should be done in a separate project, and the web site can reference the other project.
I avoid web sites like the plague, so I've never had to make this work, but consider that web sites don't build. Instead, various things are built on the fly, when the site is used. There will be no Reference.cs file in a web site.

In more recent versions of Visual Studio the using statement for these web service references also has had to include the project name.
Using cptScarlet's original code example, change the first line to look like this:
// add the service reference
using MyProject.ServiceReference1;
When you type in your project name, the class and/or namespace of the objects created in the web reference should show up in the intellisense.

Related

Routing for Single Page Application in ASP.NET Core

I have a Single Page Application written in JavaScript, and I use HTML5 history API to handle URLs on the client side. This means any URLs sent to the server should cause the server to render the same page.
In the ASP.NET MVC 5 I wrote this code to do this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.Add(new Route("{*path}", new MyRouteHandler()));
}
}
public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return WebPageHttpHandler.CreateFromVirtualPath("~/index.cshtml");
}
}
This worked really well. No matter what URL the server gets, it renders index.cshtml. Note that I am able to use a .cshtml file (as opposed to an .html file) which means I can have some C# code to dynamically change what .js scripts are included, append version numbers to .css files, and so on. What's more, I didn't have to implement controllers and views and so on just to render this .cshtml file.
Now we come to the question: How do you do this in ASP.NET Core? I have been reading the documentation, but I don't see how to render a .cshtml file without adding controller classes, view folders and other rigmarole.
Anyone know the equivalent code in ASP.NET Core?
Currently to run a CSHTML page "the normal way" in ASP.NET Core requires using ASP.NET Core MVC.
However, there is a planned feature that is being worked on that is somewhat similar to ASP.NET (non-Core) Web Pages, where you can have standalone CSHTML files. That feature is being tracked here: https://github.com/aspnet/Mvc/issues/494 (and as far as naming for the new feature, that is being tracked here: https://github.com/aspnet/Mvc/issues/5208).
There's also a sample of how to render an MVC view to a string (e.g. to generate an email, report, etc.), and that sample is available here: https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.RenderViewToString
But to use this sample in the scenario you describe, you'd have to do some extra plumbing to wire it up as its own middleware (not a lot of plumbing; just a little!).
It's also worth noting that in your scenario you probably don't want all URLs going to this one view, because you still need the static files middleware running first to handle the CSS, JS, images, and other static content. Presumably you just want all other URLs to go to this dynamic view.

ASP.NET Web.Api plugin architecture

Can you suggest me some articles or code samples about plugin architecture in web api?
Currently I'm thinking about this scenario: to have 1, centralized api gateway, where every client sends request, and have different applications controllers in Plugins folder. If someone wants to add new service, writes it's own controllers and puts dll files in Plugin folder.
For locating controller classes at run time, you can write an assembly resolver, like this.
public class MyAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
// Add all plugin assemblies containing the controller classes
assemblies.Add(Assembly.LoadFrom(#"C:\Plugins\MyAssembly.dll"));
return assemblies;
}
}
Then, add this line to the Register method in WebApiConfig.
config.Services.Replace(typeof(IAssembliesResolver), new MyAssembliesResolver());
With this, the request will still need to be sent to the individual controller even though the controller classes can come from assemblies in the plugin folder. For example, if MyAssembly.dll in the plugins folder contains CarsController, the URI to hit this controller will be /api/cars.

Access webservice on pageload

I'm developing a website in VB.Net, visual studio 2010, that requires accessing a webservice to access the user's login information.
They are logged in through a separate page and when they redirect to my page I access their credentials through the webservice and then handle the session through my own scripts.
What I need to know is:
Can a webservice be checked on pageload if a condition isn't met?
I have not worked with webservices before and have no clue how to add parameters or how to get a value from it. Is it possible to add the reference to my login class(or a class in general)?
I have added a reference through the visual studio: website -> add web reference
But this simply generated a bunch of files and I can't find a good tutorial online about how to use the generated references/files.
I thought it was supposed to generate some class files, however it added a folder (.discomap) with the following types:
.disco
.wsdl
.xsd
Finally, can I test this webservice (which is online and running) on my local host?
Thanks!
It sounds like you're wanting to call these services from your code behind.
When you added a web reference it should have generated a bunch of class files you can use to call the service methods. You should be able to do this from localhost.
A WCF service call from the code behind would look something like this
ServiceReference1.Service1Client client = new
ServiceReference1.Service1Client();
string returnString;
returnString = client.GetData(Param);
label1.Text = returnString;

Error on run with simple ActionFilterAttribute

Started writing a simple filter to pull some stuff from request on each action load, copied some code from other stackoverflows that looks like so:
public class TestKeyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.Request.Properties.ContainsKey("test"))
{
// do stuff
}
}
}
Then added the attribute with the rest:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
filters.Add(new TestKeyAttribute());
}
On run, results in this error:
The given filter instance must implement one or more of the following filter
interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.
Most of the links I've found relate to MVC 3, and this seems to work; I am however using MVC 4 and using Web API - is there some other way I need to register the attribute now?
Just a note: I don't want the filter attached to Web API controllers (adding it to GlobalConfiguration.Configuration.Filters does work, though), but rather the normal web controllers.
Edit: I know I can get this working by inheriting from IActionFilter instead and using OnActionExecuting, I'm just curious why this approach doesn't work, since a bunch of tutorials seem to say it should.
I had the same error and was puzzled as ElmahHandledErrorLoggerFilter does implement IExceptionFilter.
After investigation, I kicked myself, I'd added the filters.Add(new ElmahHandledErrorLoggerFilter()); to the MVC site config under the FilterConfig class. Adding config.Filters.Add(new ElmahHandleErrorApiAttribute()); instead to the WebApiConfig class works.
Note: I'm using WebAPi v1 here but I've configured a v2 project in the same way.
The reason this doesn't work is that your filter is a WebAPI filter, which is not interchangeable with Mvc filters. The Mvc and WebAPI FilterAttribute and related classes and interfaces have many of the same names (which is why the tutorials appear to say this should work), but they live in different namespaces. Mvc filter classes live in System.Web.Mvc and WebAPI classes live in System.Web.Http.
Further reading here: https://stackoverflow.com/a/23094418/22392
When using MVC4, the project where your custom attribute resides must contain a reference to the System.Web.Http.Common library. I added it to my project using NuGet and hey presto! the error goes away.

How to consume different proxyclass versions (production or test) of ASMX webservice

I've got an ASMX webservice as a separate project in Visual Studio 2005. In pursuit of "assembly separation" per a CODE Magazine tutorial, my proxy class is in a separate class library project containing no code of mine - just a web reference named ASMXproxy with the associated reference.cs, app.config, .disco and .wsdl files. Thus, when compiled I have a FileServiceProxy.dll.
For consuming this WS, I have a web app project called FileServiceDemo in this same solution. It has no web reference but instead a "regular" reference to FileServiceProxy.dll. In my default.aspx.cs file, I gain access to my WS via these snippets:
using FileServiceProxy.ASMXproxy;
public partial class _Default : System.Web.UI.Page
{
ASMXproxy.FileService brokerService;
protected void Page_Load(object sender, EventArgs e)
{
try
{
brokerService = new ASMXproxy.FileService();
So while things work OK this way, I find it awkward when I want to test a deployed version or make changes to a "localhost" version. I can't simply make changes to the app.config:
<applicationSettings>
<FileServiceProxy.Properties.Settings>
<setting name="FileServiceProxy_ASMXproxy_FileService" serializeAs="String">
<value>http://localhost/TRIMBrokerService/FileService.asmx</value>
</setting>
</FileServiceProxy.Properties.Settings>
</applicationSettings>
In short, when I need to publish my web app to another server, I have to change the web reference in proxy class and rebuild it. Then when I want to debug it on my localhost, I have to change the web reference back to localhost (as above).
Ideally, I would like to expose some sort of choice (e.g. radio buttons or a textbox for altering a URL at runtime) in my web app demo project such that I could have a "late binding" of sorts for the desired FileServiceProxy.dll to be used at runtime. Others have sketched proposals "using config files" but I am stuck on how to do that. It appears to me that I would have to have an additional project and hence another DLL - perhaps FileServiceProxyPROD.dll - but this seems awkward and even then I'm not sure what else I'd have to do.
Actually, you can use the same reference. Just change the Url property of the proxy instance:
using (var svc = new WebServiceProxy())
{
svc.Url = realUrl;
var result = svc.ServiceMethod();
}

Resources