我创建了一个wcf服务,但是我遇到了一些错误但无法解决。 在IService1.cs中,我有以下代码。
[OperationContract]
List<RecommendPlace> getSearchCoords(string search);
[DataMember]
public string search
{
get { return strValue1; }
set { strValue1 = value; }
}
public List<RecommendPlace> getSearchCoords(string search)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&searchVal=" + search + "&returnGeom=1");
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
string coords;
bool check = true;
JObject doc = JObject.Parse(@result);
using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(result)))
{
while (jsonReader.Read())
{
if ((string)jsonReader.Value == "ErrorMessage")
{
check = false;
}
}
}
if (check)
{
coords = result;
}
else
{
coords = "error";
}
return coords;
}
Service1.svc.cs中的结果是:
public List<RecommendPlace> getSearchPlace(string search)
{
RecommendPlaceMethods rp = new RecommendPlaceMethods();
return rp.getSearchCoords(search);
}
似乎无法获得坐标。帮助!
答案 0 :(得分:1)
错误错误1 'WcfService1.Service1' does not implement interface member 'WcfService1.IService1.getSearchCoords(string)'
准确地告诉您问题所在。您的班级Service1
未实施getSearchCoords(string)
。
在您发布的Service1.svc.cs
代码中
public List<RecommendPlace> getSearchPlace(string search)
但在IService1.cs
你有:
[OperationContract]
List<RecommendPlace> getSearchCoords(string search);
getSearchPlace
与getSearchCoords
不同。
另外,为什么界面中有[DataMember]
(IService1
)和方法实现?您的界面中应该没有 的实现,只是定义。实现接口的类负责方法和/或属性中的逻辑。
看起来应该是这样的:
IService1.cs
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
List<RecommendPlace> getSearchCoords(string search);
}
}
Service1.svc.cs
namespace WcfService1
{
public class Service1 : IService1
{
public List<RecommendPlace> getSearchCoords(string search)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&searchVal=" + search + "&returnGeom=1");
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
string coords;
bool check = true;
JObject doc = JObject.Parse(@result);
using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(result)))
{
while (jsonReader.Read())
{
if ((string)jsonReader.Value == "ErrorMessage")
{
check = false;
}
}
}
if (check)
{
coords = result;
}
else
{
coords = "error";
}
return coords;
}
}
}