Linux上使用mono的不同输出和win7上的visual studio调用web服务

时间:2012-05-09 12:40:48

标签: web-services f# mono

我使用Exchange Web服务从Exchange邮件服务器中提取附件。 当我用单声道调用linux上的代码时,某个文本附件包含一些混合字符串。

喜欢这样

“sam winglin vz”成为“sainglin vz” - >所以它缺少“m w”。 我在一个150kb的文件中看到这个约3次。 linux输出与Windows输出相比缺少3个字节。

当我从visual studio中提取它时,文本附件是完美的。

就像这个例子 Save attachments from exchange inbox

我应该从哪个方向来解决这个问题?

代码:

#r "Microsoft.Exchange.WebServices.dll"

open Microsoft
open Microsoft.Exchange.WebServices.Data
open System
open System.Net

type PgzExchangeService(url,user,password) =
    let service = new ExchangeService(ExchangeVersion.Exchange2007_SP1,
                                      TimeZoneInfo.CreateCustomTimeZone("Central Standard Time",new TimeSpan(-6, 0, 0),"(GMT-06:00) Central Time (US & Canada)","Central Standard Time"))


    do
       ServicePointManager.ServerCertificateValidationCallback <- ( fun _ _ _ _ -> true )
       service.Url <- new Uri(url)
       service.Credentials <- new WebCredentials(user, password, "domain")

    member this.Service with get() = service
    member this.InboxItems = this.Service.FindItems(WellKnownFolderName.Inbox, new ItemView(10))
    member this.GetFileAttachments ( item : Item ) =        
           let emailMessage = 
               EmailMessage.Bind( this.Service, 
                                  item.Id, 
                                  new PropertySet(BasePropertySet.IdOnly,    ItemSchema.Attachments))
           item, emailMessage.Attachments |> Seq.choose (fun attachment -> match box attachment with  
                                                                       | :? FileAttachment as x -> Some(x) | _ -> None)   

let mailAtdomain = new PgzExchangeService("https://xx.xx.XX.XX/EWS/Exchange.asmx", "user", "passw")

let printsave (item : Item ,att : seq<FileAttachment>) =
    if (Seq.length att) > 0 then
        printfn "%A - saving %i attachments" item.Subject (Seq.length att)        
        att |> Seq.iter ( fun attachment -> printfn "%A" attachment.Name 
                                            attachment.Load(@"/tmp/test/" + attachment.Name ) )   

// filter so we only have items with attachements and ...
let itemsWithAttachments = mailAtdomain.InboxItems                            
                           |> Seq.map mailAtdomain.GetFileAttachments 
                           |> Seq.iter printsave

由于TimeZoneInfo

中的错误,代码无法在带有mono的Windows上运行

此示例代码在Linux上运行,但在Windows上运行。因为TimeZoneInfo错误。 但有了这个代码,可以在linux上提取附件。 尝试csv附件,看看结果是否相同。我放松了数据!每个线路大约3个字节

如果您需要提供问题的示例csv附件

,请给我发邮件

这是我用于测试的c#版本。从VS2010运行它是完美的,但在Linux上使用mono,附件的大小错误,缺少一些字节?!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.Net;

namespace Exchange_SDP_Attachment_Extracter
{
    public class PgzExchangeService
    {
        public void Extract()
        {            
            ExchangeService service = new ExchangeService  (ExchangeVersion.Exchange2007_SP1,TimeZoneInfo.Local);
            service.Credentials = new NetworkCredential("user", "pass", "domain");
            service.Url = new Uri("https://xx.xx.xx.xx/EWS/Exchange.asmx");
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
            foreach (Item item in findResults.Items)
            {
                EmailMessage e = EmailMessage.Bind
                                 (service,
                                   item.Id,
                                   new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
                foreach ( Attachment att in e.Attachments )
                {

                    if (att is FileAttachment)
                    {
                        FileAttachment fileAttachment = (FileAttachment)att;
                        fileAttachment.Load(@"/tmp/testsdp/" + fileAttachment.Name);                        
                    }
                }

            }
        }
    }

    class Program
    {
         static void Main(string[] args)
         {
             PgzExchangeService pgz = new PgzExchangeService();
             pgz.Extract();
         }
      }
}

1 个答案:

答案 0 :(得分:0)

我的建议是尝试使用十六进制编辑器检查文本附件。这三次事件有些不寻常。在我们任何人向您推荐一系列行动之前,您需要了解这三条线的共同点。