VS 2010 Intellisense Issue - c#

I'm working on a C# library class which is being consumed by an ASP.NET 4.0 Web Forms application. In my class, I'm trying to access the HttpRequest.Application object as described here:
http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx
This documentation says it is in the System.Web namespace but even when I add a reference in my library project, it is still not available to me.
The only way I can get to the ApplicationPath property is by using:
HttpContext.Current.Request.ApplicationPath;
What's going on?

ApplicationPath is not a static property on HttpRequest, which is why you have to access it using the instance HttpContext.Current.Request. If you don't want to use HttpContext.Current.Request you could always pass the HttpRequest object in to your class library from your ASP.NET web forms.
For example (from your Page_Load):
protected void Page_Load(object sender, EventArgs e)
{
var myClass = new MyClass();
myClass.MyMethod(this.Request);
}

Related

Call WebAPI to .NET Framework from .NET Core

I have backend project on ASP .NET Core. I write a web api into this project. Also, I have UI project on .NET Framework. I want to call web apis from .NET Frameowork and use it. What's best approches is it?
Seems you are trying to call Web API from your Windows Form application.
You can try this way:
On your Windows from Button Click event you can write this code snippet
private void Button2_Click(object sender, EventArgs e)
{
HttpClient _httpClient = new HttpClient();
string uri = "http://localhost:11951/api/MsAgent/GetMSAgentByAlias?alias=TestParam";
var _http_response = _httpClient.GetAsync(uri);
_http_response.Wait();
var _read_response = _http_response.Result.Content.ReadAsStringAsync();
_read_response.Wait();
if (_read_response.Result.Contains("No Data Found!"))
{
// Your Logic what you would like to do when no response
}
var data = JsonConvert.DeserializeObject<MSAgentViewModel>(_read_response.Result);
//Do what you wants to do with your API response
//Can Bind Data With From Property
}
This is one way, you can make the API Service Class in diffrent class its up to you. Lot of best practice out there. Let me know if you require more help on this.

Custom Nancy bootstrapper not being called

I am building a Nancy based application which uses the NancyHost as the embedded web server.
I am also trying to serve some static content, by creating a custom bootstrapper as described in their documentation here.
However, the problem I see is that this custom bootstrapper class is never instantiated and the ConfigureConventions method is never called. Is there any specific action that I have to do in order to make sure this custom bootstrapper is registered?
The custom bootstrapper code below:
using Nancy;
using Nancy.Conventions;
using System;
namespace Application
{
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions conventions)
{
Console.WriteLine("test");
conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("client", #"client"));
conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("lib", #"lib"));
base.ConfigureConventions(conventions);
}
}
}
It turns out that I had to pass the reference to my CustomBootstrapper in the NancyHost constructor.
var host = new NancyHost(new Uri(JsonHelper.URL), new CustomBootstrapper(), config);
host.Start();
Is your bootstrapper in another assembly?
Please check IncludeInNancyAssemblyScanning
in
Documentation

My class lost its methods during serialization

What's my Problem
Object returned from the ASMX service is used in Silverlight application. Class has methods but the result from the ASMX WebMethod does not show methods on the object.
Tell me more
here is my class
public class Dog
{
public string Name{get;set;}
public void Bark();
}
here is the WebMethod
[WebMethod]
public List<Dog> Findlabrador()
{
blah blah blah
return list_of_labrador;
}
the silverlight code
void LabradorFetchCompleted(object sender, LabradorFetchCompletedEventArgs e)
{
var list_of_labrador = e.Result;
foreach(var labradorDog in list_of_labrador)
{
labradorDog.Bark();
//** WTH my labrador can't BARK** Bark method is not shown in intellisense there is compilation error if i explicitly specify
}
}
I am a programmer not a layman
Ok hmm, let me put in your words. Here are steps for you to reproduce the issue
Create a Silverlight Application project ( Let VS create Website to host the application)
Create a Silverlight Class library create the Dog class inside it
Compile the Silverlight Class library to assembly(Dog.dll)
Add reference to Dog.dll silverlight assembly to the silverlight application project
Add a WebService application to the project ( DogService.asmx note the asmx extension)
Add a reference to the Silverlight Dog.dll assembly for the DogService
return hardcoded List<Dog> class from a WebMethod inside it
Add a reference from the Service to Silverlight application, create a instance of proxy client and invoke the method
Watch as your Dog too can't Bark :(
Methods are never serialized. Only data. Your methods, events, indexers, constructors, etc, will never be serialized.
You should not be using ASMX services anyway. Use WCF instead. WCF, among other things, gives you the ability to share datatypes between the client and service. This would allow something like "serializing methods": the same methods could be used both on the client and server.
You are supposed to define all common classes using portable class libraries, http://msdn.microsoft.com/en-us/library/gg597391.aspx
And then when consuming the web service within Silverlight, you should ask the proxy generator to reuse those classes. That makes sure you get all the functions.
Web service definition (WSDL) only takes care of fields/properties. Methods are not transferred over the wire.

Calling a Java Web service from silverlight throws an exception

After my previous question HERE, I found the solution (well, part of it).
Here's the code for Java part:
#WebService
public class MyWebService
{
#WebMethod
public String myMethod()
{
return "Hello World";
}
#WebMethod
public int Add(#WebParam(name="a") int a,
#WebParam(name="b") int b)
{
return a + b;
}
public static void main(String[] args)
{
String address = "http://127.0.0.1:8023/_WebServiceDemo";
Endpoint.publish(address, new MyWebService());
System.out.println("Listening: " + address);
}
}
And Here is the Silverlight part:
private void SearchResultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyWebServiceClient proxy = new MyWebServiceClient();
proxy.AddCompleted += proxy_AddCompleted;
proxy.AddAsync(2, 3);
}
void proxy_AddCompleted(object sender, AddCompletedEventArgs e)
{
txtSearch.Text = e.Result.ToString();
}
But when I run this, e.Result throws an exception.
What am I missing/ How should I solve it?
Note that this code runs perfectly in C# Console application (when it's not async). But when I run the async code, it doesn't work.
Thanks in advance.
I guess you are getting a System.ServiceModel.CommunicationException when trying to access the Java Webservice from Silverlight.
There is nothing basically wrong with your code, and it should also work with async calls in C# Console App.
The main problem is that Silverlight (as a browser plugin) enforces some security restrictions that prevent a Silverlight Application to talk to another server than the one loaded from (defined by server name and port) without further configuration. This behaviour can be configured as described here (also search for "silverlight cross-domain calls" or "silverlight cross domain policy").
This restrictions (normally) do not apply to desktop or console applications so they'll work fine with the same web service.
To make your code work you need to host the Silverlight Application inside the same "project" / website than your webservice (so I suppose, the self-hosting webservice won't work and you need to switch to Java web project where the webservice is to be hosted). As the Silverlight Application basically consists of an enclosing HTML file plus the referenced binaries, you can host it on any server, e.g. Apache Tomcat.
Hope this helps.

How to use web service from the net

I have Visual Studio 2010, using c# and I want to add this web service to my site:
http://www.webservicex.net/geoipservice.asmx
I have added the web reference as "getip"
and my code is
protected void Page_Load(object sender, EventArgs e)
{
getip.GeoIPService yourip = new getip.GeoIPService();
Label4.Text = yourip.GetGeoIPContext().ToString();
}
But when the site loads the only thing appearing in my label is "getip.GeoIP "
First of all, you shouldn't use Web References anymore. Use "Add Service Reference" instead. Web References use the old "ASMX" technology. All new work should use the WCF technology, even if you're calling a .asmx web service.
Second, the service has returned you an object of the type getip.GeoIP. There's no doubt properties in that object. Try
getip.GeoIP geo = yourip.GetGeoIPContext();
And then type "geo." and see what Intellisense tells you is in there. Alternatively, stop after that line in the debugger, and look inside to see what you want to put into your label.

Categories

Resources