【发布时间】:2013-03-20 21:27:04
【问题描述】:
我正在关注如何在位于 here. 的 WCF 服务中添加授权的指南
现在我的问题是,当我创建服务并从中删除 .DoWork() 方法时,我收到一条错误消息:
'Testing.HelloService' 没有实现接口成员'Testing.IHelloService.DoWork()
这显然是因为我删除了它,但是需要它吗?在指南中,它基本上说要从中删除 .DoWork() 方法,所以我猜写它的人错过了一些东西。
当我创建它的服务时,它会将 HelloService 和 IHelloService 文件添加到项目中。我需要对 IHelloService 添加更改吗?
这里是 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";
}
}
}
这是来自 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();
}
}
【问题讨论】:
-
你需要从接口中移除
DoWork方法,否则任何没有实现DoWork的实现类都会抛出该错误。 -
您还需要在界面中添加
[OperationContract]string HelloWorld();。
标签: c# asp.net .net asp.net-mvc wcf