Question

Alternative to using svcutil.exe? (Can't create interface for ServiceReference)

Asked by: dparkes

Hello, this is a very beginner question.  In learning about WCF and webservices, I've been using svcutil.exe to generate two files like the instructions tell me to do when I go to the service.svc webpage.  That's been working fine.

But it is cumbersome to have to regenerate those files every time an update is done to the service.

Is creating a service reference (through the VS designer) an alternative to using this command line technique?

In VS I can create a service reference that finds the operations in my service, but I cannot figure out how to use them.

For instance, I have a helloworld operation that simply returns a string.

I create ServiceReference1 through the designer in VS.  I know ServiceReference1.IService contains helloworld, but I am unable to create the object necessary to use it.

When I try this:
ServiceReference1.IService sr = new ServiceReference1.IService();

It tells me "Cannot create an abstract class or interface for 'myApp.ServiceReference1.IService".

Is there a way to get this to work, or am I approaching this all wrong?

This may just be a beginners coding issue, where I need to add something to the Service class, to get the interface to work, but I'm not sure.

//Service.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.OleDb;
 
// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config.
 
 
public class Service : IService
{
    public string helloWorld()
    {
        return "Hello World!";
    }
}
 
 
 
//IService.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
 
// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
    [OperationContract]
    string helloWorld();
}
                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-08-17 at 22:43:33ID23655485
Topics

Web Services

,

C# Programming Language

,

WebApplications

Participating Experts
2
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. abstract interface
    Does it make any sense if i declare an interface as abstract. If yes, What is the difference between ordinary interface and thise one. Please in detail.
  2. difference between Interface and Abstract Class??
    Can anyone tell me internal difference between abstract class and interface. When to use interface and when to use abstract classes?? answer me in detail.. I know basic difference.. Thanks in advance..
  3. Abstract class
    Design question. Lets say I have a base class Display and 2 abstract classes, Display1 and Display2. The functions are similar such as loadConfigFile, ProcessFile and DisplayFile. Of course, the functionality is different in the ProcessFile and DisplayFile. Would using ab...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: kaylanreilorPosted on 2008-08-18 at 00:06:51ID: 22249882

It is more convenient to the developper to create a reference on a web service with Visual Studio because then Visual Studio creates the necessary proxy class for you behind the scene. Moreover, to update the web service interface the only required operation is then to right-click on the reference and click refresh.

 

by: kaylanreilorPosted on 2008-08-18 at 00:34:51ID: 22249996

Concerning your 2nd question, it seems that your class implementing your interface doesn't provide the whole required implementation as if some method defined on the interface were missing. Nevertheless that doesn't appear in the code snipped.
Perhaps it has to do with changing the interface definition and not updating properly the output of its compilation when building your class implementing it... Or the code snipped is not complete ?...
First you should resolve the refrence issue.

 

by: dparkesPosted on 2008-08-18 at 05:52:38ID: 22251315

Kavlan, I think it's complete.  I just took out a few other operations, datacontracts, and methods that did not pertain to helloWorld.  But I agree that it looks kind of sparse, as if something must be missing, so I'll look at other implementations and see what I can figure out.  It works when compiling using svcutil, but as I said above it's not letting me do it the other way by creating the interface in VS.

I will get back with you when I find something.

 

by: dparkesPosted on 2008-08-18 at 19:59:58ID: 22256983

Actually I checked against a fresh website and WCF service I created as a test.  It doesn't look like anything is missing.

It just occured to me though, that I'm using VS Web Developer Express for this project.  Creating a WCF service application is a little different than the full version of VS.  The full version has the publish option which creates a dll and the express version doesen't.

Perhaps the dll is needed to interface with the visual studio c# client application designer.

So do you think this could just be a limitation of the express version?

 

by: kaylanreilorPosted on 2008-08-18 at 22:25:07ID: 22257370

So you're using express... well, you cannot publish your website for example but, if you develop the web service and the client application in the same solution, do you mean that you cannot add a reference on the web service through the solution explorer like shows the attached file ?

 

by: kaylanreilorPosted on 2008-08-18 at 22:31:13ID: 22257385

... because according to this, http://msdn.microsoft.com/en-us/library/8wbhsy70.aspx, the only limitation seems to do with the debugging session.

BTW:
ServiceReference1.IService sr = new ServiceReference1.IService();
There is no constructor for your interface IService since it is... an interface. So should instanciate an object of a class IMPLEMENTING the interface:
ServiceReference1.IService sr = new ServiceReference1.Service();
Does that better work ?

 

by: dparkesPosted on 2008-08-19 at 05:45:36ID: 22259211

Kevlin, thanks, that turned out to be a pretty simple oversight on my part.  This is what worked:

ServiceReference1.IService sr = new ServiceReference1.ServiceClient();

ServiceClient was also in the intellisense menu.

To be honest I don't understand how that ServiceClient class object was automatically created, but there it was plain as day for me to select, and I just overlooked it.  Right there in front of me.  Thanks.

 

by: dparkesPosted on 2008-08-19 at 05:47:21ID: 31487524

Thank you very much, it really helps to have people to ask these naive questions.

Sorry for mispelling you name, I tried to go back and edit the posts but it would let me.

Thanks again.

 

by: dparkesPosted on 2008-08-19 at 10:01:04ID: 22261638

A slight correct, I think the client should be created more like this:

ServiceReference1.ServiceClient sr = new ServiceReference1.ServiceClient();

 

by: kaylanreilorPosted on 2008-08-19 at 10:26:39ID: 22261884

I disagree with you. If you define an interface it is to use it. The interface exposes the methods and properties that are needed in your client application according to your architecture design. At this point you only know that you need a component that exposes A() for example. To really have this component instanciated in your client app, you can re-use a component which is not only designed to give A() but also B(). But your client app doesn't care about B(). Your client app shouldn't even know anything about B() (for security issue for example, or for restricting accroding licences that you sold...). That is why interfaces design are done before implementation.

Let's imagine :
interface IA { void A();}
interface IB { void B();}
class myComponent : public IA, public IB
{
   // coding A() and B()
}

And then in the client app:
IA iA = new myComponent;
iA->... here only A() is available not B() !

An instance X of a class Y that implements an interface Z is an object which proposes the "services" defined (contracted) on the interface's definition. That's also why some microsoft interfaces definitions are public.
That is the same type of difference than between classes and object.

 

by: navyjax2Posted on 2008-10-15 at 12:30:45ID: 22724811

I'm trying to do the same kind of thing here, and the

ServiceReference1.ServiceClient sr = new ServiceReference1.ServiceClient();

doesn't work for me or make much sense.  Where does the "ServiceReference1 come from, and how did it get an "intellisensed" ServiceClient behind it, if it wasn't even instantiated yet?  Error occurs saying "The type or namespace name 'ServiceReference1' could not be found (are you missing a using directive or an assembly reference?)"

I have a web service I've referenced at http://localhost:4500/NewMsgSvc.  It has an event that will bring back message IDs if I give it a date to draw them back from, "GetEventsSince(DateTime since)".  All I wanna know is how do you call this interface for that web service I was given.  The web service has been referenced in my project.

// IExternalSearchEventService.cs - the interface I was given
 
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Data;
using System.Web;
using System.Runtime.Serialization;
using System.Diagnostics;
using Tester;
 
namespace Tester {    
 
    [ServiceContract(Namespace = "http://localhost:4500/NewMsgSvc")]
    public interface IExternalSearchEventService {
 
        [OperationContract()]
        List<SmartSearchEvent> GetEventsSince(DateTime since);
    }
 
// The class that will call the routine, Program.cs:
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel;
using System.Runtime.Serialization;
using Tester;
 
namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            Check_For_Messages();
        }
 
 
       static int Check_For_Messages()
        {
            try
            {
                // Instantiate & open the connection
                SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Messaging;Integrated Security=SSPI;Trusted_Connection=Yes");
                sqlConn.Open();
 
                // Retrieve the Last_Run time to know how far back the parent URL has to go
                SqlCommand sqlLastRun = new SqlCommand("SELECT Last_Run FROM Sys", sqlConn);
                DateTime lastRunDate = (DateTime)sqlLastRun.ExecuteScalar();
                Console.WriteLine("Last Ran:" + lastRunDate);
 
                // Get an array of messages - this is where I'd like help
                IExternalSearchEventService searchSvc = new IExternalSearchEventService.ServiceClient(); 
                searchSvc.GetEventsSince(lastRunDate);
 
                // Close the connection
                sqlConn.Close();
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 1;
            }
        }
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:

Select allOpen in new window

 

by: navyjax2Posted on 2008-10-15 at 14:39:49ID: 22726206

Ok, think I figured it out...  Here's what I learned.  This is what they should have said to answer the question -

When you use an interface, you have to do these things first:

1)  The class on your client (the class calling the interface) should match the class on your web service. (Changed my "class Program", above, to "class NewMsgSvc")
2)  The class on your client needs to inherit the interface name (in my case, IExternalSearchEventService - changed my "class NewMsgSvc" from step #1 to "class NewMsgSvc : IExternalSearchEventService")
3)  You have to have a DataContract section in its own class, with DataMembers, so you can get back results.  This can be a section under the interface, just in its own class.  (added - see below)
4)  You must create a function within your client class that a) instantiates the service class into a variable (  NewMsgSvc searchSvc = new NewMsgSvc();  ), and b) returns the value of that variable.InterfaceEvent(parameterYouWantToPass)  (added - see below)
5)  You must call that function in your client class
(I used:                    
List<NewMessages> msgList = GetEventsSince(lastRunDate);
)

I wish that had just been spelled out.  Hopefully someone will be able to put this information to good use.



    // New DataContract section
    [DataContract(Namespace = "http://localhost:4500/NewMsgSvc")]
    public class NewMessages
    {
        [DataMember]
        private string msgId;
        public string MsgId
        {
            get { return msgId; }
        }
    }
 
// function to call the web service, in the client class
        public List<NewMessages> GetEventsSince(DateTime lastRunDate)
        {
            NewMsgSvc searchSvc = new NewMsgSvc();
            return searchSvc.GetEventsSince(lastRunDate);
        }
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

Select allOpen in new window

 

by: navyjax2Posted on 2008-10-17 at 16:47:58ID: 22746368

Actually, I found out that this was NOT the best approach.  When calling the methods of the web service into my client, I kept getting errors saying I hadn't implemented the methods from the interface!  How can that be, when I was calling the methods using the class name that was instantiated as an object and everything?  Well, so I found a BETTER, EASIER way than all this....

Using these two sites for guidance....

http://www.csharphelp.com/archives2/archive356.html
http://articles.techrepublic.com.com/5100-10878_11-5768122.html

I created the DOS Batch script below to help me create a DLL out of my Web Service.  All you do is input the URL to the web service (i.e. http://localhost/MyWebSvc/Service.asmx) and the name of the class (MyWebSvc) when prompted.  You will get the proxy class and the DLL file.

If you use Windows Authentication, you need to add
    <identity impersonate="true"></identity>
to your web.config file for your Web Service.

Take the DLL file and put it in your bin folder.  Add it as a Refernce in your project.  Then do this:

                MyWebSvc anyOldName = new MyWebSvc();
                anyOldName .Url = "http://localhost:4500/MyWebSvc/Service.asmx";
                    // or use System.Configuration.ConfigurationSettings.AppSettings["MyWebSvc"];
                   // if you configure it in the web.config of your Web Service first

                 // For Windows Authentication...
                searchSvc.PreAuthenticate = true;
                searchSvc.Credentials = System.Net.CredentialCache.DefaultCredentials;

                    // Then call it, like this ---
                    string myResult = anyOldName.GetMyResult(parameterToMakeTheSvcWork);
                    Console.WriteLine(myResult);

I wish I had found out all that stuff in one place instead of dealing with all this .......  Hopefully no one, now, will have to go through what I did.....

Take care,
Tom

echo off
cls
cls
set /P svc="Input the URL to your web service's Service.asmx file: "
cd \
cd "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin"
wsdl /l:CS /protocol:SOAP %svc%?WSDL
set /P name="Type the name of the service's class' name (no .cs on end): "
move %name%.cs C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\%name%.cs
cd \
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
csc /t:library %name%.cs /reference:System.Web.Services.dll /optimize
move %name%.dll C:\%name%.dll
move %name%.cs C:\%name%.cs
echo Your DLL and Proxy Class are waiting for you at
echo.
echo   C:\%name%.dll
echo.
echo. and
echo.
echo.  C:\%name%.cs
echo.
pause
cls
exit

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...