How to upload files to HttpRequest - c#

I'm making with VS2015, WPF and C# a test application in which we can test and debug an ASP.Net web service application.
Both projects belong to the same solution.
Because some methods in the webservice would normaly called by an URL including some parameters i had to create the HttpRequest programatically before calling such methods.
In one of these cases it is necessary to add files to the request.
Fortunately i have found in this forum the following thread:
How to create a file to populate HttpContext.Current.Request.Files?
The version from StuartLC seemed to be very practical but i have a little problem with the calling of InvokeMethod.
The objects which use InvokeMethod (types object and HttpFileCollection) do normally not have that method.
Have i forgotten some references or assemblies in my test project?

Related

Implementing GraphQL in MVC solution - Never returns

I have added a few classes to send requests and handle responses from an external GraphQL web API, which use the GraphQL package from NuGet as well as HttpClient to connect to the API. The classes live in one project of my solution ("providers"). I have also added Test classes and methods to test the functionality of the service classes. The Tests all work as expected - results come back and they match the same queries made in Postman. Having success with the tests, I added references to the service classes to the main project which is an MVC web application in order to supplement user search capabilities - so calling a method from the same project as the Api helper classes within a controller in the MVC application. This is when I started having issues.
As an aside I did not have to add any of the GraphQL packages to the Test project, and it was able to work fine by just referencing the provider project that contains the api helper class. So it seems a bit like overkill in the first place that I had to add all that to the MVC project.
Initially, I was getting errors such as "ReadAsAsync doesn't exist" (this is a method of the System.Net.Http.HttpContent class I think?). This method was used in other classes that consume other external web services using HttpClient in the same project. In fact the method that was failing was nearly identical to ones in other classes that had been working for years at this point. So I thought it was strange it stopped working at this time. I eventually concluded it had something to do with all the dependencies added by adding the GraphQL nuget packages to the "providers" project.
I then added those same GraphQL packages to the MVC project to see if that resolved the issue. This added some issues where it couldn't find some System.IO libraries. I had seen this before in another project where I had to update the .NET version and the solution in that case was to remove the offending dependentAssembly entries in the web.config file. I did that here. Now, I load the web application in debug mode, and can click through to the page where the controller action to search using the new API class is located. The original search actions still work - I am able to get results using them. But the new API method does not. It does not throw an exception. It does not return results. The execution simply stops or seems to stop after the call to the API happens. The GraphQLHttpClient.SendQueryAsync just never comes back. It could just be that I'm not waiting long enough, but I gave it 10 minutes at one point for a query that runs in seconds in the test project, and also in seconds in postman. The application remains operational, though. I can still use other functions of the site after clicking on this.
I guess the question is - how can I find out what the problem is here? Or correct it in some way? I'm currently trying removing different dependentAssembly entries in the MVC project but that isn't really working out very well so far. Some of those entries are needed by the application. But not all of them.
** UPDATE **
I was able to work with some people on another site to resolve the issue.
Basically I was calling:
public static GqlResponse GetGqlResponse(string name){
var gqlResponse = client.SendQueryAsync<GqlData>(gqlRequest).Result;
return gqlResponse;
}
In a method in a class in one project, from an MVC controller action in a different project. Because of this, it caused a deadlock.
public ActionResult Index (){
var gql = myotherproject.myclass.GetGqlResponse("moo");
return View(gql);
}
The solution to this issue was to not try to make an asynchronous process completely synchronous. I changed the whole code pathway to be asynchronous.
public class MyGplClass{
public static async Task<GplResponse> GetGplResponse(string name){
// build the request, client, etc...
var gqlResponse = await client.SendQueryAsync<GqlData>(gqlRequest).ConfigureAwait(false);
// process response...
return gplResponse;
}
}
And in the MVC project:
public class MyController : BaseController{
public async Task<ActionResult> Index(){
var gqlr = await project2.MyGplClass.GetGplResponse("moo");
return View(gplr);
}
}
To reference for the future, hopefully this link stays alive: Don't Block on Async Code. But the gist of it is that because I was using the .Result property of the Task object created by calling the Async method, this was causing a deadlock within my Web Application (ASP.NET, .NET Framework), because of the way these sorts of web applications work. Because normally this problem had not happened for me, I was unprepared for it and I did not know what was happening when it happened this time. Hopefully this solution helps some people who have a similar problem as I stated in my question.

Access current request headers from inside a dll function

I think this should be fairly simple but I can't find anything on google on this for some reason
I have a WebAPi 2.0 controller (type:ApiController) that can read the read the request headers
Request.Headers.GetValues("SomeHeader");
In the same project I have another class (AccessFactory) that I would like to access the headers from.
Reason being that I would not want to do the same code for every function call to read all the needed headers at the point where they are actually needed.
Within a separate project / DLL you can still get access to the current HTTP context (and thus the request headers) using the System.Web.HttpContext.Current object.
As an aside, I would add that if this DLL is meant to be re-usable across different applications, then consider whether it will be used on non-web applications, and if so you should avoid making dependencies on a web-based context like this. Instead you might consider passing in the header values you need as simple types, or in a custom object. That way they can indicate the context, but a non-web app could pass in equivalent contextual values of its own. But that's a separate design decision depending on the exact purpose and how you intend to use this DLL library in future.

How to read Web Method and parameters name from Web Service

I am C#.NET developer. I am writing a application which can call web service dynamically. I got one web service that is written in Java. This web service returns only web method name in WSDL file but doesn't returns their input/output parameters details. They keep all details in another XSD link. That xsd link present in WSDL file. I want to know that how I can read that XSD file and how we can read Web Method name and their input and output parameters.
I am looking help on priority.
this is sample link: http://www.java-tutorial.ch/api-doc/ArticleService.html#src.N10068
Thanks in advance.
Rajeev
XSD files are valid XML which means you can parse it with classes from the System.Xml or System.Xml.Linq namespaces. I'm assuming when you say you're dynamically calling the web service that you can't rely on a static WSDL reference in Visual Studio
Assuming you are using Visual Studio and are looking to generate proxy classes to consume the web service you need to add a web reference to the WSDL. You can find instructions on how to do this at microsoft.
This process will generate the classes required to interact with the webservice, including all the methods and properties.
This isn't the only way to generate the proxy classes, but IMHO it's the easiest.
Here is a working example of what you can end up with:
I develop against an ERP system called Netsuite. They provide a WSDL at https://webservices.netsuite.com/wsdl/v2013_2_0/netsuite.wsdl.
Following the instructions in the link above I show the following:
I can now call the class like any other. In order to view the classes now available,
view the reference in the Object Browser. The image below shows all the classes, each one of course you can click into to see what properties are available.

How do I create a simple SOAP server using C# in Visual Studio 2010?

I was trying to follow the instructions here but they seem to quickly be getting very specific to Sharepoint, as they go along further. I just want a general tutorial for creating a non-sharepoint SOAP starter projects, and Google is not my friend, at the moment.
It seems that some of the above steps are applicable.
So far I have:
A solution 'WebSite1' created with File -> New Website.
A 'ClassLibrary1' project in C#, with System.Web.Services added to References.
a key02.snk (strongname key file) file node in the classlibrary1.
No code implementing any SOAP server methods yet.
No idea how to add code to the above, and then build and run, and have a soap hello-world type method.
I'd like to implement a simple HelloWorld method that takes at least one parameter. The resulting service could be queried for its WSDL with a url like this:
http[s]://localhost/myfirstsoapserver/helloWorldMethod1.asmx?WSDL
If my guess above is right, the above Url would be usable by any tool that can import WSDL.
My goal is rapid prototyping and mocking up of various other SOAP interfaces that I need to deal with, and I'm hoping that C#+visualStudio2010+IIS is a reasonably easy way to do that.
First create a new project as a web application:
Then add a new item of type web service:
Have you considered using WCF?: http://msdn.microsoft.com/en-us/library/bb386386.aspx

How to call a webservice in the same website?

I have to call a webservice published in the same website the caller aspx is.
When I try to "Add a Web Reference" the editor does not show the webservice methods from the generated namespace.
Do I have to use the "Add Web Reference" or is there another way because the webservice is in the same website?
Visual Studio 2005, C#
Thanks,
Eduardo
I used the way that Andy Rose wrote in comments:
Instantiate the webservice class and call the methods directly because it is accessible inside the project (no need to add web reference).
There are ways to trick it... You can deploy the app with the web service, then add reference to the deployed location.
Another idea is to just start it using ctrl-f5 so as to not start the debugger as well, then add a reference to your localhost:/.asmx
This will get all of the configuration information created in your web.config which, of course, you can modify later as necessary.
The Webreference itself is just a proxy implementation so that VS can pretend it knows how your webservice will react to calls to it (so you can compile), you can actually code without them (though it is much harder :)).
You have a few of options, that I can think of, if you want to do this:
Create your webservice as a separate project, but deploy it to the same location. You will get mixed binaries in the bin directory, but otherwise it should work fine. In this way you can deploy each separately as needed.
Consider pulling them apart into two separate applications. This might not be available for whatever reason.
Create a stub webservice in your main project, which only has the function definitions (return type and parameters) and deploy that, then generate your webservice against that. You can then begin your development against the prototype and fill it in as necessary
Do the two-step shuffle as Chris suggested. First create a approximate representation of your webservice, deploy it. Second create your proxy against it, create your web app, deploy it and test. Repeat as necessary.
Once your proxy is created you can change the URL it points to in the proxy bindings.

Categories

Resources