【问题标题】:Sending a XML File through WCF Fails通过 WCF 发送 XML 文件失败
【发布时间】:2013-10-24 21:42:39
【问题描述】:

我一直在尝试将 XML 文件从我的 WCF 发送到我的项目,但运气不佳。一旦 WCF 完成响应并将其发送到电话,我的程序就会抛出异常。我希望有人可以帮助我,因为我一直在寻找答案,但一无所获。 (该程序将 XNA 用于 Windows Phone 应用程序)

[System.Net.WebException]   {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)}  System.Net.WebException


public string EndHighScoreList(System.IAsyncResult result) {
                object[] _args = new object[0];
                string _result = ((string)(base.EndInvoke("HighScoreList", _args, result)));
                return _result;
            }

IService.cs

 [ServiceContract]
    [XmlSerializerFormat]
    public interface IService
    { 
        [OperationContract]
        void ParseScore(HighScore score);
        [OperationContract]
        string HighScoreList();
    }
    public class HighScore
    {
        [XmlElement]
        public UInt32 m_rank;
        [XmlAttribute]
        public string m_name;
        [XmlAttribute]
        public UInt32 m_score;
    }

Service.svc

public string HighScoreList()
        {

            XmlSerializer ser = new XmlSerializer(typeof(HighScore));
            using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("App_Data/Highscores.xml"), FileMode.OpenOrCreate))
            {
                return ser.Deserialize(fs).ToString();
            } 
        }

这是请求的代码

void globalRecieve(object obj, DodgeService.HighScoreListCompletedEventArgs e)
        {
            try
            {
                string result = e.Result;
                using (TextReader reader = new StringReader(result)){ 
                    XmlSerializer xml = new XmlSerializer(typeof(List<DodgeService.HighScore>));
                    foreach (DodgeService.HighScore sco in xml.Deserialize(reader) as List<DodgeService.HighScore>)
                        highScores.Add(sco); 
                } 
            }catch(Exception exception){
                string error = exception.Message;
            } 
        }


        protected override void Initialize()
        {
             service = new DodgeService.ServiceClient("BasicHttpBinding_IService");
        service.HighScoreListAsync(null);
        service.HighScoreListCompleted += new EventHandler<DodgeService.HighScoreListCompletedEventArgs>(globalRecieve);

            base.Initialize();
        }

【问题讨论】:

  • “我的程序抛出异常” - 请出示。
  • System.ServiceModel.ni.dll 中发生了“System.ServiceModel.CommunicationException”类型的异常,但未在用户代码中处理
  • @JoshuaWaring 好吧,你去吧。您的解决方案是处理该异常。
  • 但是问题是什么 D:如果我只是随机地 Catch and Ignore 它仍然可以工作吗?
  • 不,不要乱抓!抓住你打电话的地方,然后检查异常!对于开发,您可以在故障中添加异常详细信息,如果您想在客户端查看服务器上出了什么问题。

标签: c# xml wcf


【解决方案1】:

我个人认为 WCF 很烂。仅配置本身就是一场噩梦,如果您更改任何内容,则必须重新构建对象,并且您所做的任何更改都必须重新进行。

您应该迁移到ServiceStack。它为您处理一切。你只写业务DTO对象的发送和接收。发送和接收文件是它的基本内容,

看到这个google search 有几个人提出类似的问题,但基于ServiceStack。 Mythz 是 ServiceStack 的项目负责人,他回答了他们的问题。它应该让你开始,你应该看到它是多么容易。

仅供参考

“服务栈文件上传”

【讨论】:

    【解决方案2】:

    错误提示:“未找到”。看起来操作HighScoreList 根本没有公开/可用。尝试在浏览器中打开路径。

    【讨论】:

    • 我使用 WCF 测试客户端测试服务,一切正常,结果与我预期的一样。当我在出现异常的应用程序中使用它时,这只是一个问题。所以确认服务正常工作,但不是应用程序,我不明白为什么。
    【解决方案3】:

    我一直有一个 Not Found 错误,因为 Windows Phone 运行它试图通过 LocalHost 连接到服务,这不起作用,因为我需要它来连接到开发 PC。解决方案是在服务器上托管 WCF 服务并连接到服务器或连接到开发 PC 的 IP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2011-12-10
      • 2021-06-14
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      相关资源
      最近更新 更多