views:

479

answers:

3

Is it possible to call a web service from an inline code block in ASP.NET without adding a web reference? If so, can you provide instructions?

+1  A: 

In general inline code can do anything code behind code can do, the code behind code is just cleaner and sepearates files.

You need first to include the proper namespaces that were generated for you when you created the WebService Proxy.

The namespace include code is a bit different in the case of inline code, here is an example

<%@ Import Namespace="System.IO" %>

If you wish to read about the differences Coding horror has a great article about it

bashmohandes
A: 

Add a web reference in a another C# project (e.g. console application). Then switch to "show all files". You'll find a hidden .cs file that contains the actual web service proxy that was created for you. (There's also a commandline tool that does the same thing, but I forgot the name)

You can copy the generated class to your .aspx into a <script runat="server"> block.

Although I don't know what you would actually need that for ;) Don't you have access to the source code of the web app?

Another solution is to create a .NET dll for the web proxy, then load this assembly dynamically using Assembly.Load(). Invoking methods dynamically is not pretty though.

chris166
-1 for suggesting any manipulation of a generated file, including copying it.
John Saunders
His method was a shortcut for running the command line tool, which is what I really did. It was the solution, and it was the only way to do it with the requirements that I had.
Mike C.
A: 

If you have the Web Service's code file (*.cs) in your App_Code folder, you won't need any reference, using, etc. However, if the Web Service is not local in your project or if you have not the code in a separate .cs file in the App_Code folder, you will need a Web Reference.

That is from my experience, and I have just used one of my Web Service's Web Method as such:

<%= new MyWebService().MyWebMethod(); %>

Hope it helps.

Maxime