WCF Service- Does not implement interface member - c#

I am following a guide on how to add authorization in a WCF service located here.
Now my problem is when I create the service and remove the .DoWork() method from it, I get an error that says:
'Testing.HelloService' does not implement interface member 'Testing.IHelloService.DoWork()
This is obviously because I removed it, but is it needed? In the guide it basically said to remove the .DoWork() method from it, so I am geussing the person who wrote it missed something.
When I create it service it adds the HelloService and IHelloService files to the project. Do I need to add changes to IHelloService?
Here is the code in HelloService.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.ServiceModel.Activation;
namespace MLA_Test_Service
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in code, svc and config file together.
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class HelloService : IHelloService
{
public string HelloWorld()
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
return HttpContext.Current.User.Identity.Name;
else
return "Unauthenticated Person";
}
}
}
Here is the code from IHelloService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MLA_Test_Service
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
[ServiceContract]
public interface IHelloService
{
[OperationContract]
void DoWork();
}
}

Your implementation needs to match your interface. If you don't want to implement the DoWork method, it needs to go from the implementation and the Interface.
In fact, you probably should just replace DoWork with the name of the method you actually want to invoke on the service and implement that method instead. It's supposed to serve as an example of how to initiate an operation on the WCF service.

its simple C#, you are inheriting an interface, you must implement all the declared methods of it in the class.
DoWork() exists in your Interface, hence implement it in your HelloService class.
Moreover, only those methods will be visible to your WCF-Service client, which will be declared in OperationContract i.e. the Interface and is marked [OperationContract]

Related

Models not recognized in namespace

Heey all,
I'm working on a Umbraco website. I try to reach my renderModels from my controller but, it says to me that type or namespace could not be found. So i try to add using namespace.Models;.
When i do this the namespace.controllers is reachable but when i try namespace.Models; it gives me the error the type or namespace name 'Models' does not exist in the namespace.
The folder Models does exist.
I tried searching online for it but could not find a awnser. Pls help
Edit 1
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using System.Globalization;
namespace MyNamespace.Models
{
public class HomeRenderModel : BaseRenderModel
{
public HomeRenderModel()
{
//
// TODO: Add constructor logic here
//
}
}
}
Controller
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using MyNamespace.Models;
namespace MyNamespace.Controllers
{
public class HomeController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var rendermodel = new HomeRenderModel(model.Content, model.CurrentCulture);
return CurrentTemplate(rendermodel);
}
}
}
Hope this helps, but basically I did the tutorial that mentions the model and controller etc. out there and had the same problem. I figured out that I had created the model folder containing the classes in the wrong place. You may have the same simple problem here.
Minimize all your folders in the project explorer, and then expand to see where you placed them. Your sub folders need to be in the project (in the 'folder' with the little globe icon, not above it with the solution properties.)

Why I cannot use HttpResponseMessage class?

I'm new to ASP.NET Web API and want to make HttpResponseMessage instance from a utility class I made. Then I made very simple class.
But following compile error occurred.
CS0246: The type or namespace name 'HttpResponseMessage' could not be
found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace myapplication.App_Code.Utils
{
public class HttpUtility
{
// compile error here
public HttpResponseMessage GetHttpResponseMessage ()
{
return new HttpResponseMessage();
}
}
}
HttpResponseMessage is available from Controller Class which was made automatically by ASP.NET but not my Utility class.
What am I missing?
I have noticed that you placed your class in App_Code folder in your project. This folder has a special purpose in ASP.NET world and should be used for shared classes and business objects that you want to compile as part of your application. So either move your class into another folder, or change its Build Action in properties section to Compile.

How to use comboBox in asmx.cs to create a Web Service?

I have basic knowledge to create a web service using asmx.cs
and consume from any client application.
I want to create a Web Service using asmx.cs in .NET 3.5 using Visual Studio 2010.
For the following program,By default it gives textBox to take input from user.
But I want to use comboBox to take input from the user.
The result will be displayed as xml output.
I want to do a Web Service program where one city from ComboBox will be taken as input
and the temperature will be shown as xml output.
This code gives only textBox as input.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string GetCityByZip(int Zip)
{
return "City Name = XYZ, Temperature = 30 Degree";
}
}
}
I think, what you're talking about is this html to test your web service methods, right?
http://3.bp.blogspot.com/_nuQwSyDoLk8/RvWnnpahW8I/AAAAAAAAAJw/UTFuJmCx5M0/s1600-h/WS3.bmp
This test ui is generated automatically based upon your service's WSDL.
The input types are based on the methods your service exposes. So if there's a method like CityInfo GetCityInfo(string cityName) in your service, there will be a textbox with input type string (as you already noticed). If you're exposing a method like CityInfo GetCityInfo(int cityId) the input type will be an int.
What's not possible, is to put a combobox there, as the service is autogenerated and has no knowledge which cities are selectable.
What you can do, is expose several methods and build a ui yourself (which you should do anyway).
public interface IYourServiceInterface
{
City[] GetCities(); // returns all possible cities
CityInfo GetCityInfo(City city); // returns detail Infos about a concrete city
}

Can't access web service methods, but can access the MethodNameCompletedEventArgs and MethodNameCompletedEventHandler

This is my first time dealing with making my own web service, and I made a basic one, which I am currently only needing to access locally:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Xml;
namespace WebService1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
#region Temp Conversion
[WebMethod]
public string F2C(int f)
{
double c = 5.0 / 9.0 * (f - 32);
return c.ToString();
}
}
}
This works when I run this and run the method through the browser. However I've added it to another project, a windows form application, via "Add Service Reference" < Advanced
But now within the project when I try to access the methods they don't show.
I do:
MyWebService.
I named the referenced service "MyWebService", and all thats available is:
F2CCompletedEventHandler
F2CCompletedEventArgs
Service1
as opposed to
MyWebService.F2C
On its own being available right away.
you need to create an Instance of the myWebService object can you show the code where you are consuming / creating the proxy and or instance..? sounds like you need to use fully qualified name in regards to the namespace + the class name
Example:
MyWebService.Service1 myService = new MyWebService.Service1();
to call the method do the following
myService.F2C(15); // for example

instantiating an object from a web service vs instantiating an object from a regular class

I have a very basic web service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
public int myInt = 0;
[WebMethod]
public int increaseCounter()
{
myInt++;
return myInt;
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
when I run that project my browser opens showing me the service:
on a different solution: (console application)
I am able to connect to that service by adding the reference:
then click on the add web reference button:
Lastly I type the url of the service I just created:
Now I am able to instantiate an object from the class Service1 from my console application as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication36
{
class Program
{
static void Main(string[] args)
{
localhost.Service1 service = new localhost.Service1();
// here is the part I don't understand..
// from a regular class you will expect myInt to increase every time you call
// the increseCounter method. Even if I call it twice I always get the same result.
int i;
i=service.increaseCounter();
i=service.increaseCounter();
Console.WriteLine(service.increaseCounter().ToString());
Console.Read();
}
}
}
why does myInt does not increase every time I call the increaseCounter method? every time I call that method it returns 1.
Services created through the older .asmx technology are not singleton instances. This means that each call you make to the server instantiates a new instance of the service each time. Two real solutions, either use static variables (eugh....), or switch to using WCF.
Becaue on the server side the class is created and disposed with EVERY call you make from the client... your client is just a "proxy" and doesn't correspond directly to an instance on the server side...
You can either make myInt static or make the server side service class a Singleton... both options would mean that myIntis shared across ALL client... or you could implement some session management to achieve a client-specific myInt... using WCF for the server side seems IMHO the best solution - it comes with configurable options for singleton, session management etc.
EDIT - as per comments:
With WCF you can have .NET-clients with session management which in turn allows you to have different (client-specific) values for myInt...
webservice instance is destroyed at the end of each method call, so that's why you always get the same result. You need some way to persist that value.

Categories