簡體   English   中英

在安裝了VS2013的地方,C#中的Web應用程序運行良好,但在WebServers中無法運行

[英]Web app in C# works good where VS2013 is installed but not in WebServers

就這樣,代碼可以在測試環境中順利運行(使用Windows 8.1,帶有SP4的Visual Studio 2013)。

關鍵是我的代碼在部署到生產環境時沒有連接到MSOnline。 當我使用Webdeploy選項進行部署時,我只是無法找出問題所在……我只是無法找到問題所在……我真的需要幫助。 它使用MSOnline和擴展模塊運行Powershell。

我確實安裝了模塊MSOnline: http : //www.microsoft.com/zh-cn/download/details.aspx?id= 39267 64位MSOnline模塊。 和這個腳本:

set A=C:\Windows\System32\WindowsPowerShell\v1.0\Modules
set M1=MSOnline
set M2=MSOnlineExtended
set B=C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules
IF NOT EXIST %B%\%M1% mkdir %B%\%M1%
IF NOT EXIST %B%\%M2% mkdir %B%\%M2%
xcopy %A%\%M1% %B%\%M1% /s /e
xcopy %A%\%M2% %B%\%M2% /s /e
pause

這將使模塊從32位准備好以64位運行。

在安裝了Visual Studio 2013的地方運行良好,但是當我將其部署到WebServers時……根本無法正常工作……我試圖在這里和互聯網上找到類似的東西,但沒有任何歡樂和運氣。感謝您能給我的任何幫助,謝謝!

這是我在ASP.NET中的代碼:

 <%@ Page Title="Domain Info" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master"     CodeBehind="DomainInfo.aspx.cs" Inherits="LSO365Portal.DomainInfo" %>
 <%@ MasterType VirtualPath="~/Site.Master" %>
 <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
 <section class="featured">
    <div class="content-wrapper">
        <hgroup class="title">

           <h1> <%= titulo %> </h1>
           <h3>Here where you'll find interesting information about your Office 365 Domains.</h3>
        </hgroup>
    </div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3> Execute Script pressing the following button</h3>
<p>
    <asp:Table HorizontalAlign="Center" runat="server">
        <asp:TableRow>
            <asp:TableCell>
                <asp:Button ID="Execute" runat="server" Text="Execute Script" OnClick="RunScript" Height="35px" Width="150px" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
  </p>
 <h3> Results</h3>
  <p>
    <asp:Table ID="SubsTable" runat="server" GridLines="Both" HorizontalAlign="Center"  CssClass="ResultTables">
    </asp:Table>
  </p>
  <h3> Elapsed</h3>
 <asp:Label ID="EnlapsedLabel" runat="server" Text=""></asp:Label>

  <h3> Status</h3>
  <p>
  <asp:TextBox ID="Stats" runat="server" Width="850px" Height="80px" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
  </p>

  </asp:Content>

這是背后的代碼:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Collections.ObjectModel; //Collection<T>
 using System.Management.Automation;
 using System.Management.Automation.Runspaces;
 using System.Text; //stringbuilder
 using System.Security; //securestring
 using System.Text.RegularExpressions;
 using System.Diagnostics; //stopwatch

namespace LSO365Portal
{

public partial class DomainInfo : System.Web.UI.Page
{
    protected const string PasswordMask = "*********************************************************************************************************************************";
    public String titulo = "Domains Information in Office 365";
    Stopwatch watch = new Stopwatch();

    StringBuilder status = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e){}
    //protected void UpdateTimer_Tick(object sender, EventArgs e)
    //{
    //    DateStampLabel.Text = watch.ElapsedMilliseconds.ToString();
    //}
    protected void RunScript(object sender, EventArgs e)
    {
        watch.Reset();            
        watch.Start();

        // Clean the Result TextBox
        Stats.Text = string.Empty;
        status.Clear();
        //check1
        status.AppendLine("watch reseted and status cleared");
        StringBuilder script = new StringBuilder();

        try
        {
            //            Import-Module MSOnline OPC #1
            InitialSessionState iss = InitialSessionState.CreateDefault();

            iss.ImportPSModule(new string[] { "MSOnline,MSOnlineExtended" });
            //check2
            //status.AppendLine("initial state and MSOnline Loaded");
            Runspace rs = RunspaceFactory.CreateRunspace(iss);
            rs.Open();
            //status.AppendLine(rs.InitialSessionState.ToString());
            //check3
            //status.AppendLine("Created and open runspace");

            Pipeline pipeLine = rs.CreatePipeline();

            string un = String.Empty;
            string pw = String.Empty;
            TextBox tbUn = (TextBox)Master.FindControl("Username");
            TextBox tbPw = (TextBox)Master.FindControl("Textpwd");

            //check 4
            //status.AppendLine("Created pipeline");
            if (tbUn != null && tbPw != null)
            {
                un = tbUn.Text;
                pw = tbPw.Text;
            }
            else
            {
                status.AppendLine("Error trying to find username and password from the master page");
            }

            //Set variables Username and password
            script.AppendLine("$username = '" + un + "';");
                                         //ConvertTo-SecureString      '    MySPW '     –asplaintext –force
            script.AppendLine("$password = Convertto-securestring " + "'" + pw + "'" + "-asplaintext -force");
            script.AppendLine("$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password;");

            // Check Username and password
            script.AppendLine("write-Output $username");
            script.AppendLine("write-Output $password");

            //connect and check connection
            script.AppendLine("Connect-MsolService -credential $cred -ErrorAction SilentlyContinue");
            script.AppendLine("if($?){ write-output 'connected' } else{write-output 'disconnected'}");


            //check 5
               //$DomainInfo = Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial
            //status.AppendLine("Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial");
            script.AppendLine("Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial");
            //Add scripts to the pipeline

            pipeLine.Commands.AddScript(script.ToString());
            //check 7
            status.AppendLine("Added script to pipeline");
            status.AppendLine("this is the script");
            status.AppendLine(script.ToString());

            //execute pipeline and get the results PSObjects
            Collection<PSObject> resultObjects = pipeLine.Invoke();
            //check 8
            status.AppendLine("Total results Objects are: " + resultObjects.Count.ToString());

            foreach (PSObject obj in resultObjects)
            {
                status.AppendLine(obj.ToString());
            }
            //Clear Script and close Runspace
            script.Clear();
            rs.Close();

            if (resultObjects.Count > 0 || resultObjects != null)
            {
                GenerateTable(resultObjects);
            }
        }
        catch (System.Management.Automation.RuntimeException ex)
        {
            status.AppendLine(ex.Message);
        }
        status.AppendLine("Ready");
        Stats.Text = status.ToString();
        watch.Stop();
        double elap = watch.ElapsedMilliseconds / 1000;
        EnlapsedLabel.Text = "Work Done in : " + elap.ToString() + " seconds";
    }

    private string SetRowHeadersName(int i)
    {
        //Name,Authentication,Capabilities,Status,isDefault,isInitial
        string text = String.Empty;
        switch (i)
        {
            case 0:
                text = "Name";
                break;
            case 1:
                text = "Authentication";
                break;
            case 2:
                text = "Capabilities";
                break;
            case 3:
                text = "Status";
                break;
            case 4:
                text = "is Default";
                break;
            case 5:
                text = "is Initial";
                break;
            default:
                text = "Error on SetRowHeadersName Function";
                break;
        }
        return text;
    }
    private string SetCellValue(int j, PSObject result)
    {

        string text = string.Empty;
        switch (j)
        {
            //Name,Authentication,Capabilities,Status,isDefault,isInitial
            case 0:
                text = result.Properties["Name"].Value.ToString();
                break;
            case 1:
                text = result.Properties["Authentication"].Value.ToString();
                break;
            case 2:
                text = result.Properties["Capabilities"].Value.ToString();
                break;
            case 3:
                text = result.Properties["Status"].Value.ToString();
                break;
            case 4:
                text = result.Properties["isDefault"].Value.ToString();
                break;
            case 5:
                text = result.Properties["isInitial"].Value.ToString();
                break;
            default:
                text = "Value not found, column:" + j;
                break;
        }
        return text;
    }
    private void GenerateTable(Collection<PSObject> objs)
    {

        int rowsCount = objs.Count;
        //The number of Columns to be generated
        const int colsCount = 6;//You can changed the value of 5 based on you requirements NextLifecycleDate,SkuId,SkuPartNumber,Status,TotalLicenses

        // Now iterate through the table and add your controls
        try
        {
            //ADD HEADER NAMES
            TableRow rowHeader = new TableRow();

            for (int i = 0; i < colsCount; i++)
            {
                //create a new header
                TableCell header = new TableCell();
                //create new label
                Label headertext = new Label();
                //set text of label
                headertext.Text = SetRowHeadersName(i);
                //set unique ID
                header.ID = "headerCell_" + i;
                header.CssClass = "tableheader";
                //add control to the header
                header.Controls.Add(headertext);
                // Add the TableCell to the TableRow
                rowHeader.Cells.Add(header);
            }
            SubsTable.Rows.Add(rowHeader);

            //Add info on PSObjects
            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {
                    TableCell cell = new TableCell();
                    Label tb = new Label();
                    string RowText = string.Empty;
                    cell.CssClass = "tablecell";
                    RowText = SetCellValue(j, objs[i]);
                    tb.Text = RowText;

                    // Set a unique ID for each TextBox added
                    tb.ID = "Row_" + i + "Col_" + j;
                    // Add the control to the TableCell
                    cell.Controls.Add(tb);
                    // Add the TableCell to the TableRow
                    row.Cells.Add(cell);
                }

                // And finally, add the TableRow to the Table
                SubsTable.Rows.Add(row);
            }

            //Sore the current Rows Count in ViewState
            ViewState["RowsCount"] = rowsCount;
        }
        catch (Exception ex)
        {
            status.Append("Error in GenerateTable() " + ex.Message);
        }
    }

}
}

可能您需要安裝Visual Studio默認安裝的某些組件(例如,正確版本的.NET或您正在使用的一種庫)。 您是否拋出任何錯誤? 您可能需要查看事件日志才能看到它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM