通过WCF发送XML文件失败

时间:2013-10-01 10:19:47

标签: c# xml wcf

我一直在尝试从我的WCF向我的项目发送一个XML文件而没有太多运气。一旦响应由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();
        }

3 个答案:

答案 0 :(得分:0)

我个人认为 WCF糟透了。单独配置是一场噩梦,如果你改变任何东西,你必须重新构建你的对象以及你必须重新制作的任何改变。

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

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

只是为了将来参考,如果谷歌搜索没有提供与我得到的相同,这里是搜索和前三个响应;

“servicestack文件上传”

答案 1 :(得分:0)

错误说:“NotFound”。看起来操作HighScoreList根本没有暴露/可用。尝试在浏览器中打开路径。

答案 2 :(得分:0)

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