Silverlight 到 Web 服务不工作?

发布于 2024-12-11 07:01:46 字数 3293 浏览 0 评论 0原文

我创建了一个简单的 Silverlight Web 应用程序表单,用于测试我为更大的项目设置的 Web 服务。该表单基本上是包含名字、姓氏和中间名的文本框,单击按钮时,代码将传递到服务器上的 Web 服务,该服务以 pdf 形式打印它们,将其保存到磁盘,然后通过电子邮件发送它。 我已经编写了所有代码,没有收到任何错误,但它不起作用。没有保存文件,也没有发送电子邮件。 这是来自 MainPage.xaml.cs 的代码:

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!");
            }

        }
    }
}

我不知道我的设置有什么问题。

I have a simple Silverlight web app form I created to test a web service I have set up for a bigger project. This form is basically textboxes that take First Name, Last Name, and Middle Name, and on button click, the code gets passed on to the web service on the server, which prints them in a pdf form, saves it to disk, then emails it.
I’ve written all the code, I don’t get any errors, but it’s not working. No file gets saved and no email gets sent.
Here’s the code from the MainPage.xaml.cs:

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;

    }
}

}

And here’s the code from my web service:

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!");
            }

        }
    }
}

I can’t tell what is wrong with what I have set up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-12-18 07:01:46
  • 从方法的开头开始“try/catch”块。可能您在服务器上有写权限错误。
  • 尝试另一种方法。您可以创建内存中的 pdf 文件,然后将其作为电子邮件发送。在这种情况下,您的程序不需要服务器上的特殊权限。更多信息:[^]
  • Start that "try/catch" block from the beginning of the method. probably you have the write permission error on the server.
  • Try another method. you can create an in memory pdf file and then send it as an email. in this case your program doesn't need special permissions on the server. more info:[^]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文