Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Docker容器中的Azure函数在Visual Studio中运行,但不从命令行执行任何操作_C#_Visual Studio_Docker_Selenium Webdriver_Azure Functions - Fatal编程技术网

C# Docker容器中的Azure函数在Visual Studio中运行,但不从命令行执行任何操作

C# Docker容器中的Azure函数在Visual Studio中运行,但不从命令行执行任何操作,c#,visual-studio,docker,selenium-webdriver,azure-functions,C#,Visual Studio,Docker,Selenium Webdriver,Azure Functions,我的目标是在docker容器中运行timer azure函数(利用Selenium Chromedriver)。当我单击此按钮时,它在VisualStudio中工作;但是,当我在命令行中从VS执行相同的docker命令时,什么也没有发生。 下面是VS执行的命令 docker run -dt -v "C:\Users\rlin\vsdbg\vs2017u5:/remote_debugger:rw" -e "ASPNETCORE_ENVIRONMENT=Developm

我的目标是在docker容器中运行timer azure函数(利用Selenium Chromedriver)。当我单击此按钮时,它在VisualStudio中工作;但是,当我在命令行中从VS执行相同的docker命令时,什么也没有发生。 下面是VS执行的命令

docker run -dt -v "C:\Users\rlin\vsdbg\vs2017u5:/remote_debugger:rw" -e "ASPNETCORE_ENVIRONMENT=Development" -p 34480:80 --name Retriever --entrypoint tail retriever -f /dev/null
这是我的文件供你参考

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/azure-functions/dotnet:3.0 AS base
WORKDIR /home/site/wwwroot
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /home/site/wwwroot
COPY . Retriever/
WORKDIR /home/site/wwwroot/Retriever
RUN dotnet build -c Release -o /home/site/wwwroot

FROM build AS publish
RUN dotnet publish -c Release -o /home/site/wwwroot

FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /home/site/wwwroot .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    AzureWebJobsStorage="StorageConnectionString"
    URL="url" \
    USERNAME="username" \
    PASSWORD="password" \
    SFTP="sftp" \
    CONTAINER="container"

WORKDIR /home/site/wwwroot
RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

WORKDIR /
CMD ["/home/site/wwwroot/bin/Retriever.dll"]
我已经坚持了好几天了,所以非常感谢您的帮助

编辑:这是ref的代码

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OpenQA.Selenium.Chrome;
using Microsoft.Extensions.Configuration;
using OpenQA.Selenium;
using System.Collections.Generic;
using Azure.Storage.Blobs;
using System.IO.Compression;

namespace Retriever
{
    public class Retriever
    {
        private IConfigurationRoot config;

        [FunctionName("Retriever")]
        public void Run(
            [TimerTrigger("0 */1 * * * *", RunOnStartup = true)] TimerInfo myTimer,
            //[HttpTrigger(AuthorizationLevel.System, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            config = new ConfigurationBuilder()
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            Retrieve(log);
            log.LogInformation("Uploaded attachments");
            //return new OkResult();
        }
        public void Retrieve(ILogger log)
        {
            log.LogInformation("Retrieving attachments");

            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--headless");
            options.AddArgument("--no-sandbox"); // Bypass OS security model
            options.AddArgument("start-maximized"); // open Browser in maximized mode
            options.AddArgument("disable-infobars"); // disabling infobars
            options.AddArgument("--disable-extensions"); // disabling extensions
            options.AddArgument("--disable-gpu"); // applicable to windows os only
            options.AddArgument("--disable-dev-shm-usage"); // overcome limited resource problems
            options.AddArgument("--whitelisted-ips");
            options.AddArgument("--proxy-server=direct://");
            options.AddArgument("--proxy-bypass-list=*");
            options.BinaryLocation = "/opt/google/chrome/google-chrome"; //Google Chrome path
            options.AddUserProfilePreference("download.prompt_for_download", false); // DO NOT prompt for download
            options.AddUserProfilePreference("download.default_directory", Path.GetTempPath()); // saves to Temp directory

            ChromeDriverService service = ChromeDriverService.CreateDefaultService("/chromedriver/", "chromedriver"); // use "/usr/bin/" and "chromedriver" in deployment

            ChromeDriver driver = new ChromeDriver(service, options, TimeSpan.FromMinutes(3));
            driver.Manage().Timeouts().ImplicitWait = new TimeSpan(5, 0, 0);

            // Login and Download Steps //
            driver.Navigate().GoToUrl(config.GetValue<string>("URL"));

            IWebElement username = driver.FindElement(By.XPath("//*[@id=\"username\"]"));
            username.SendKeys(config.GetValue<string>("USERNAME"));

            IWebElement next = driver.FindElement(By.CssSelector("body > mc-login > div > div.container > div > div > div.panel.panel-default.panel-shadow.login-panel > div > div > div > form > button"));
            next.Click();

            IWebElement password = driver.FindElement(By.CssSelector("#password"));
            password.SendKeys(config.GetValue<string>("PASSWORD"));

            IWebElement login = driver.FindElement(By.CssSelector("body > mc-login > div > div.container > div > div > div.panel.panel-default.panel-shadow.login-panel > div > div > div > form > button"));
            login.Click();

            IWebElement inbox = driver.FindElement(By.LinkText("Inbox"));
            inbox.Click();
            
            ICollection<IWebElement> emails = driver.FindElements(By.CssSelector("tr"));
            System.Threading.Thread.Sleep(new TimeSpan(0, 0, 2));
            foreach(IWebElement email in emails)
            {
                if (email.Displayed)
                {
                    email.Click();

                    IWebElement view = driver.FindElement(By.LinkText("View"));
                    view.Click();

                    string filename = driver.FindElement(By.CssSelector("body > div.page-container.with-sidebar.full-height.snap-content > div.main-content.full-height > div.ng-isolate-scope > div > div > div.container-fluid.full-height.fill-container.mainarea.ng-scope.mc-tab-unique-main > mc-list-detail > div > div.col-xs-12.col-sm-7.col-md-8.col-lg-8.hidden-xs.full-height.animate-active.no-padding > div > div.dynamic-full-height.horizontal-scroll.vertical-scroll.panel-half-margin-top-negative.ng-scope > div.pull-left.full-width.ng-scope > detail > div:nth-child(4) > div > mc-thumbnail > div > div:nth-child(2) > ul > li > span > span:nth-child(2)")).GetAttribute("innerHTML");
                    string filepath = Path.Combine(Path.GetTempPath(), filename);

                    IWebElement download = driver.FindElement(By.LinkText("Download"));
                    download.Click();
                    System.Threading.Thread.Sleep(new TimeSpan(0, 0, 2));
                    UploadToBlobStorage(log, filepath, filename);
                }
            }
            driver.Close();
            driver.Quit();
            driver.Dispose();
            service.Dispose();
        }
        public void UploadToBlobStorage(ILogger log, string filepath, string filename)
        {
            BlobServiceClient serviceClient = new BlobServiceClient(config.GetValue<string>("SFTP"));
            BlobContainerClient containerClient = serviceClient.GetBlobContainerClient(config.GetValue<string>("CONTAINER"));
            BlobClient client = containerClient.GetBlobClient("uploads/" + filename + ".txt");

            ZipArchive z = ZipFile.Open(filepath, ZipArchiveMode.Update);
            foreach(ZipArchiveEntry e in z.Entries)
            {
                client.Upload(e.Open(), overwrite: true);
            }
            z.Dispose();
            if(File.Exists(filepath)) { File.Delete(filepath); }
        }
    }
}

使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用OpenQA.Selenium.Chrome;
使用Microsoft.Extensions.Configuration;
使用OpenQA.Selenium;
使用System.Collections.Generic;
使用Azure.Storage.Blobs;
使用系统IO压缩;
命名空间检索器
{
公共类检索器
{
私有IConfigurationRoot配置;
[函数名(“检索器”)]
公开募捐(
[TimerTrigger(“0*/1****”,runnstartup=true)]TimerInfo myTimer,
//[HttpTrigger(AuthorizationLevel.System,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
config=new ConfigurationBuilder()
.SetBasePath(环境.CurrentDirectory)
.AddJsonFile(“local.settings.json”,可选:true,重载更改:true)
.AddenEnvironmentVariables()
.Build();
检索(日志);
登录信息(“上传的附件”);
//返回新的OkResult();
}
公共作废检索(ILogger日志)
{
登录信息(“检索附件”);
ChromeOptions选项=新的ChromeOptions();
选项。添加参数(“--headless”);
options.AddArgument(“--no sandbox”);//绕过操作系统安全模型
options.AddArgument(“开始最大化”);//以最大化模式打开浏览器
options.AddArgument(“禁用信息栏”);//禁用信息栏
options.AddArgument(“--disable extensions”);//禁用扩展
options.AddArgument(“--disable gpu”);//仅适用于windows操作系统
options.AddArgument(“--disable dev shm usage”);//克服有限的资源问题
options.AddArgument(“--whitelisted ips”);
options.AddArgument(“--proxy server=direct://”);
options.AddArgument(“--proxy pass list=*”);
options.BinaryLocation=“/opt/google/chrome/google-chrome”;//谷歌浏览器路径
options.AddUserProfilePreference(“download.prompt_for_download”,false);//不提示下载
options.AddUserProfilePreference(“download.default_目录”,Path.GetTempPath());//保存到临时目录
ChromeDriverService=ChromeDriverService.CreateDefaultService(“/chromedriver/”,“chromedriver”);//在部署中使用“/usr/bin/”和“chromedriver”
ChromeDriver驱动程序=新的ChromeDriver(服务、选项、时间跨度从分钟(3));
driver.Manage().Timeouts().ImplicitWait=newtimespan(5,0,0);
//登录和下载步骤//
driver.Navigate();
IWebElement username=driver.FindElement(By.XPath(“/*[@id=\“username\”]);
username.SendKeys(config.GetValue(“用户名”);
IWebElement next=driver.FindElement(通过.CssSelector(“body>mc login>div>div.container>div>div>div.panel.panel-default.panel-shadow.login-panel>div>div>div>form>button”);
下一步。单击();
IWebElement password=driver.findelelement(By.CssSelector(“#password”);
password.SendKeys(config.GetValue(“密码”));
IWebElement login=driver.FindElement(通过.CssSelector(“body>mc login>div>div.container>div>div>div.panel.panel-default.panel-shadow.login-panel>div>div>div>form>button”);
login.Click();
IWebElement收件箱=driver.FindElement(By.LinkText(“收件箱”);
收件箱。单击();
ICollection emails=driver.FindElements(By.CssSelector(“tr”));
系统线程线程睡眠(新的时间跨度(0,0,2));
foreach(电子邮件中的IWebElement电子邮件)
{
如果(显示电子邮件)
{
email.Click();
IWebElement视图=driver.FindElement(By.LinkText(“视图”);
视图。单击();
字符串文件名=driver.findelelement(By.CssSelector("正文>div.page-container.with-sidebar.full-height.snap-content>div.main-content.full-height>div.container-fluid.full-height.fill-container.mainarea.ng-scope.mc-tab-unique-main>mc列表详细信息>div>div.col-xs-12.col-sm-7.col-md-8.col-lg-8.hidden-xs.full-height.animate-active.no-padding>div>div.dynamic-全高。水平滚动。垂直滚动。面板半边距-上负。ng-scope>div.pull-left.full-width.ng-scope>detail>div:nth child(4)>div>mc缩略图>div>div:nth child(2)>ul>li>span>span>span>span:nth child(2)”).GetAttribute(“innerHTML”);
字符串filepath=Path.Combine(Path.GetTempPath(),文件名);
IWebElement下载=driver.FindElement(By.LinkText(“下载”);
下载。点击();
系统线程线程睡眠(新的时间跨度(0,0,2));
UploadToBlobStorage(日志、文件路径、文件名);
}
}
driver.Close();
driver.Quit();
driver.Dispose();
service.Dispose();
}
public void UploadToBlobStorage(ILogger日志、字符串文件路径、字符串文件名)
{
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . .
RUN dotnet build -c Release -o /app/build
FROM build AS publish
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
[...]