Saturday, March 24, 2012

about Atlas

Hello:

I'm new in Atlas, and I need help to understand :)

My scenario is that I have one gridview (vsnet 2005) and a button. When I click button, I call remote web service (http:outerIP/myservice.asmx) that returns data used for gridview's datasource.

I have seen examples for calling from javascript to web service, but I don't understand proccess.

1- I add the web reference to my vs2005 (with atlas template) project.

2- I modify this code that template created in aspx.

<asp:ScriptManagerrunat="server"ID="scriptManagerId"><Scripts><asp:ScriptReferencePath="CallWebServiceMethods.js"/><-----</Scripts><Services><asp:ServiceReferencePath="http://OUTERIP/webservices/MyService.asmx"InlineScript="false"/></Services></asp:ScriptManager>

my ask are:

    this .js file "CallWebServiceMethods.js"... how is generated? is it a copy of real web service?Must I make this .js?Is not intelllisense in javascript for calling webservice methods?If i call webservice method from client side in javascript... how can I to send to grid this received datasource?

Thanks a lot and regards!

For your four questions, I'd like to share my ideas with you for reference.
1.The Services collection allows you to wire up web services that can be consumed via client side script. The ScriptManager will generate proxy JavaScript scripts that allow the developer to use similar syntax on the client as is used on the server. The supported bits will not support calls to Windows Communication Foundation (WCF) services, however, this functionality will be added to the non-supported CTP builds in the future.
The AJAX Extension includes an HttpHandler that is used to generate the proxy scripts and handle JSON serialization called Microsoft.Web.Script.Services.ScriptHandlerFactory. Internally the HttpHandler routes .asmx calls that require JSON serialization to the AJAX networking stack.Calls to .asmx that don't use the special syntax (http://.../foo.asmx/js/methodname) are routed to the original ASP.NET 2.0 .asmx handler. Note that this means if a developer created a custom .asmx handler, when AJAX web services are enabled on a site the ScriptHandlerFactory will instead always fall back to System.Web.Services.Protocols.WebServiceHandlerFactory. There is no capability to chain the AJAX handler to a custom .asmx handler.
You wire up the web services to the <ScriptManager> by adding a ServiceReference to the Script Manager. The following demonstrates how to add a service reference to foo.asmx. You can also do this by accessing the Services collection in the Property pane of the IDE.
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="~/.../foo.asmx" />
</Services>
</asp:ScriptManager
This will result in javascript objects being instantiated in the browser which are proxy objects for corresponding .NET classes in foo.asmx. The proxy objects are used
a)To make asynchronous requests in JavaScript to the asmx web methods
b)To create and initialize instances of proxies of server data types. This allows developers to pass complex objects as parameters as well as handling complex objects in the response.

ServiceReference Properties
You add a ServiceReference control to the <Services> section for each web service you need to access via JavaScript. The reference drives the process of requesting the proxy script on the client. The proxy script is covered later in this lesson. The ServiceReference control has two properties: InlineScript and Path.
InlineScript - The InlineScript attribute on the ServiceReference tag determines whether the script for generating the Javascript proxies is included within the page, or is downloaded as a separate script resource. The default value is false.

<asp:ServiceReference Path="~/.../foo.asmx" InlineScript="true" /
When InlineScript is set to false (or is absent) then the proxy generation script is obtained by a separate request to http://.../foo.asmx/js which is handled by the AJAX HttpHandler. Since the script can be cached in the browser, this option is preferable when multiple asmx pages use the same service reference.

When InlineScript is set to true the proxy generation script is included as an inline script block within the page. This can improve performance by reducing the number of network requests, particularly if there are many service references within the page and other pages do not reference the same services.

Path: The Path property specifies the URL for accessing the web service. The ServiceReference tag can only be used for local services (i.e. services within the same domain). Local services can be addressed by a relative, app-relative, domain-relative or absolute path. For domain-relative or absolute paths it is up to the developer to ensure that the path does correspond to the same domain, and so will actually work. The proxy generation code will initialize the path property to the corresponding domain-relative path.

This means that if you enter have the following:
<asp:ServiceReference InlineScript="true" Path="http://mydomain.com/Parts.asmx" /
The proxy script will be requested using the full URL:
http://mydomain.com/Parts.asmx/js

However the path in the proxy script will call the webservice using the virtual path of /Parts.asmx.

If you specify InLineScript="true", you have to either use a virtual or relative path. You can also use the ~/ syntax to have ASP.NET build the app relative path for you. You will get the following error message if InLineScript="true" and the Path is pointing to a different domain than the ASPX page was requested from:

The path "http://test.com/TestServices/WebService.asmx" is not supported. When InlineScript=true, the path should be a relative path pointing to the same web application as the current page.

Path to external services
For external use of services, using a ServiceReference with an absolute URL will fail, as specified above. It is recommend that developers do the following:
?Download the script, using http://mydomain.com/Parts.asmx/js
?Open the .js file and set the correct value of the path property. If the web service class is called "WebService" and the site is http://test.com, you would do the following:

Change: WebService.set_path("/TestServices/WebService.asmx");
To: WebService.set_path("http://test.com/TestServices/WebService.asmx");

?Reference the script in the <ScriptManager>
<asp:ScriptManager ID="MyManager" runat="server">
<Scripts>
<asp:ScriptReference Path="WebServiceDifferentURL.js" />
</Scripts>
</asp:ScriptManager
If you choose to do this, users will get the following message when they click the button letting the user know that they are sending a request to a different site:

Simple Demo
Proxy generation script
When the service request tag does not specify InlineScript = "true", a separate HTTP request is made to the asmx to download the proxy script. This is done using a special URL request syntax as mentioned previously.

http://.../foo.asmx/js

This request returns the proxy generation scripts that instantiate the specific JavaScript proxy objects for foo.asmx. This is done to allow developers to write JavaScript code using namespaces and classes which are not inherently available in JavaScript. This also abstracts the serialization of complex types from the developer. The content type of the returned proxy script is set to "application/x-javascript".

Proxy objects graph corresponding to namespace
The proxy generation script will create a Web Service proxy object corresponding to any Web Service class in foo.asmx that has a server attribute [ScriptService] and contains one or more methods marked with the [WebMethod] attribute.

[Microsoft.Web.Script.Services.ScriptService]
public class PeopleServices : System.Web.Services.WebService {
}
2.No.This is not necessary to call a web service through javascript,which only provides a method to consume web service from the client side.You can call a web service method according to the traditional way.
3.There are a lot of Ajax controls which can consume web service and have ServicePath and Servicemethod properties to specify a web service.This is intellisense.But it is not .js file.
4.Calling Web Services in JavaScript
Now that we've talked about setting up the ScriptManager and how the proxy functionality works, let's take a look at how developers will leverage this technology. We'll look at passing both simple and complex data type and handling errors. During the demos, we'll take a look at the network traffic being passed to and from the server.

"Fire and Forget" Invocation
If the web service class on the server includes a web method that does not return data, you can call the web service without having to handle a response. This is the simplest web method call that can be made from the client. For example, your application has the following web method:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public void NoReturn()
{
//do work here
System.Threading.Thread.Sleep(2000);
}
}

The following JavaScript can be used to invoke that web method

function RunWebService()
{
Parts.NoReturn();
}

If the webservice is in a custom namespace, you would need to fully
qualify the call to the webservice. For example, if the WebService
has a namespace of MyCustomNameSpace.WebServices, the above JavaScript
becomes:

function RunWebService()
{
MyCustomNameSpace.WebServices.Parts.NoReturn();
}

If you want to get the dataset which is returned by?a?web?service,you?can?call?it?according?to?the?traditional?way?and?bind?it?to?asp:GridView.
If?you?would?like?to?consume?some?web?service?methods?through?javascript?from?the?client?side,try?to?use?Ajax?control?toolkit?which?are?useful?for?you.?

Wish the above can help you.

great answer. thanks and regards!

No comments:

Post a Comment