首页 新闻 会员 周边

wcf 部署问题

0
悬赏园豆:10 [已解决问题] 解决于 2012-09-18 16:42

程序使用的是rest,而不是soap。

直接在项目里运行.svc没有问题,但是部署到IIS上后,uri是http://localhost:8089/CallTaxi/CallTaxiService.svc/CallTaxi/json/1可以返回一个字符串,但是网址是http://localhost:8081/CallTaxi/CallTaxiService.svc/CallTaxi/Phone?Longitude=116.35698&Latitude=46.23578 的却没有反应,或者显示“无法找到该网页”。

这时怎么回事?

ttssrs的主页 ttssrs | 初学一级 | 园豆:82
提问于:2012-09-17 21:08
< >
分享
最佳答案
0

1,不要使用.svc,请通过 ServiceRoute 来添加路由规则;

2,第一种uri是rest标准的uri格式,而第二种不是,因此在你的接口中你需要添加 UriTemplate 来说明参数模板。

UriTemplate = "Phone?Longitude={v1}&Latitude={v2}"

CallTaxi(decimal v1,decimal v2);

收获园豆:8
Launcher | 高人七级 |园豆:45045 | 2012-09-18 09:29

非常感谢。请问使用rest是不是一定要用WCF REST Service Template 40(CS)?前几天一直用wcf service application建工程,开始还能在网页访问,现在都访问不了了。我是按照您说的uritemplate格式写的。

ttssrs | 园豆:82 (初学一级) | 2012-09-18 10:21

@ttssrs: 这个模板我没用过,我一直是手动编码的。用wcf service applicaiton建工程也可以,提示无法找到网页的话,说明没有匹配上。

你仔细看你的两个url,我不知道第一个的 json 是怎么添加上的。

或许你可以把你的接口定义贴出来。

Launcher | 园豆:45045 (高人七级) | 2012-09-18 10:25

@Launcher: 我的接口定义是这样的:

[OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Phone?Longitude={longitude}&Latitude={latitude}")]
        string SearchTaxi(string longitude, string latitude);

这个部署到iis上后始终无法显示。

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
        string JSONData(string id);这个接口是个测试例子,没有逻辑,只是return一个字符串,可以工作。

我最近才接触wcf,一直在一个人探索。您有demo可以分享吗?

ttssrs | 园豆:82 (初学一级) | 2012-09-18 12:58

@ttssrs:接口没有问题,出错后的HTTP代码是多少?

 在IE中输入 http://localhost:8081/CallTaxi/CallTaxiService.svc?wsdl  测试服务是否启动。

你可以去MSDN上找到例子。

Launcher | 园豆:45045 (高人七级) | 2012-09-18 13:04

@Launcher: 是400错误,无法找到该网页

ttssrs | 园豆:82 (初学一级) | 2012-09-18 13:27

@ttssrs: 你访问这个 http://localhost:8081/CallTaxi/CallTaxiService.svc?wsdl 也是 HTTP - 400吗?

Launcher | 园豆:45045 (高人七级) | 2012-09-18 13:31

@Launcher: 嗯

ttssrs | 园豆:82 (初学一级) | 2012-09-18 13:32

@ttssrs: 你的服务没启动,你首先得确定你正确部署了服务。

Launcher | 园豆:45045 (高人七级) | 2012-09-18 13:33

@Launcher: 服务是在哪里启动?为什么我访问json/{id}就没有问题呢?

ttssrs | 园豆:82 (初学一级) | 2012-09-18 13:35

@ttssrs: 如果你要反问我为什么json/{id}没问题的话,那我只能告诉你:请你先睁大眼睛,看看

http://localhost:8089/CallTaxi/CallTaxiService.svc/CallTaxi/json/

http://localhost:8081/CallTaxi/CallTaxiService.svc/CallTaxi/Phone?Longitude=116.35698&Latitude=46.23578

有什么不同?

Launcher | 园豆:45045 (高人七级) | 2012-09-18 13:42

@ttssrs: 这样的问题,你应该问你自己,你的自己比对有什么不同。

Launcher | 园豆:45045 (高人七级) | 2012-09-18 13:44

@Launcher: 端口我后来都改成8081了,我确定这个不是问题。

ttssrs | 园豆:82 (初学一级) | 2012-09-18 13:50

@ttssrs: http://msdn.microsoft.com/zh-cn/library/ms733766.aspx

先把你的服务启动了再说。

Launcher | 园豆:45045 (高人七级) | 2012-09-18 14:08

@Launcher: 我找到原因了,是数据库字段名写错了。给您添麻烦了。

ttssrs | 园豆:82 (初学一级) | 2012-09-18 16:41
其他回答(1)
0

[ServiceContract(SessionMode = SessionMode.Allowed)]
    public interface IMobileService
    {    

     [OperationContract]
          [WebGet(UriTemplate = "pc/{computerId}", ResponseFormat = WebMessageFormat.Json)]
          ComputerDto GetComputer(string computerId);

  }

public class MobileService : IMobileService
{

  public const string Route = "Device";

  public ComputerDto GetComputer(string computerId){}

}

 

Global.asax中加入:

void Application_Start(object sender, EventArgs e)
{

  WebServiceHostFactory factory = new WebServiceHostFactory();
      RouteTable.Routes.Add(new ServiceRoute(MobileService.Route, factory, typeof(MobileService)));

}

收获园豆:2
Pretty_Cool | 园豆:157 (初学一级) | 2012-09-18 14:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册