0
我有一個簡單的Silverlight Web應用程序窗體,我創建它來測試我爲更大項目設置的Web服務。這種形式基本上是帶有名字,姓氏和中間名的文本框,並且在按鈕單擊時,代碼將被傳遞到服務器上的Web服務,該服務以PDF格式打印出來,保存到磁盤,然後通過電子郵件它。 我寫了所有的代碼,我沒有得到任何錯誤,但它不起作用。沒有文件被保存並且沒有電子郵件被髮送。 下面是MainPage.xaml.cs中的代碼:Silverlight到Web服務無法正常工作?
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using ExampleSilverlightApp.ServiceReference1;
namespace ExampleSilverlightApp
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.newpdfRequestBody proxy = new ServiceReference1.newpdfRequestBody();
proxy._1_FirstName = textBox1.Text;
proxy._1_lastName = textBox2.Text;
proxy._1_middlename = textBox3.Text;
}
}
}
下面是從我的web服務代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.IO;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace ExampleSilverlightApp.Web
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public void newpdf(string _1_FirstName, string _1_lastName, string _1_middlename)
{
string filename = @"C:\Temp\" + _1_FirstName + _1_lastName + ".pdf";
iTextSharp.text.Document d = new iTextSharp.text.Document(PageSize.A4, 72, 72, 172, 72);
PdfWriter.GetInstance(d, new FileStream(filename, FileMode.Create));
d.Open();
d.Add(new Paragraph(string.Format("First Name:", _1_FirstName)));
d.Add(new Paragraph(string.Format("Last Name:", _1_lastName)));
d.Add(new Paragraph(string.Format("Middle Name:", _1_middlename)));
d.Close();
try
{
MailAddress SendFrom = new MailAddress("[email protected]");
MailAddress SendTo = new MailAddress("[email protected]");
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
MyMessage.Subject = "new test";
MyMessage.Body = "heres a new test!";
Attachment attachfile = new Attachment(filename);
MyMessage.Attachments.Add(attachfile);
SmtpClient emailClient = new SmtpClient("smtp.live.com");
emailClient.Send(MyMessage);
}
catch (FileNotFoundException)
{
Console.WriteLine("File Lost!");
}
}
}
}
我分不清什麼是錯什麼我已經成立了。
我沒有看到誰調用newpdf服務方法,你確定你的發佈代碼是否已經完成? –
您是否嘗試將您的'_1_FirstName'重命名爲簡單的推薦約定'firstName'? –
您的xap文件是否與服務器位於同一個域中?沒有服務器上的clientaccesspolicy.xml,Silverlight不允許跨域訪問。看到這裏,如果是這種情況:http://msdn.microsoft.com/en-us/library/cc197955%28VS.95%29.aspx – Leo